head 1.1; branch 1.1.1; access ; symbols micropython-1-5-2-base:1.1.1.1 MICROPYTHON:1.1.1; locks ; strict; comment @# @; 1.1 date 2016.01.14.01.38.49; author agc; state Exp; branches 1.1.1.1; next ; commitid Q8liT0v6zMF95QQy; 1.1.1.1 date 2016.01.14.01.38.49; author agc; state Exp; branches ; next ; commitid Q8liT0v6zMF95QQy; desc @@ 1.1 log @Initial revision @ text @******************************* :mod:`usocket` -- socket module ******************************* .. module:: usocket :synopsis: socket module This module provides access to the BSD socket interface. Functions --------- .. function:: socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP) Create a new socket using the given address family, socket type and protocol number. .. only:: port_wipy .. note:: SSL sockets need to be created the following way before wrapping them with ``ssl.wrap_socket``:: import socket import ssl s = socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_SEC) ss = ssl.wrap_socket(s) .. function:: socket.getaddrinfo(host, port) Translate the host/port argument into a sequence of 5-tuples that contain all the necessary arguments for creating a socket connected to that service. The list of 5-tuples has following structure:: (family, type, proto, canonname, sockaddr) The following example shows how to connect to a given url:: s = socket.socket() s.connect(socket.getaddrinfo('www.micropython.org', 80)[0][4]) Exceptions ---------- .. data:: socket.error .. data:: socket.timeout Constants --------- .. data:: socket.AF_INET family types .. data:: socket.SOCK_STREAM .. data:: socket.SOCK_DGRAM socket types .. data:: socket.IPPROTO_UDP .. data:: socket.IPPROTO_TCP .. data:: socket.IPPROTO_SEC protocol numbers class socket ============ Methods ------- .. method:: socket.close Mark the socket closed. Once that happens, all future operations on the socket object will fail. The remote end will receive no more data (after queued data is flushed). Sockets are automatically closed when they are garbage-collected, but it is recommended to close() them explicitly, or to use a with statement around them. .. method:: socket.bind(address) Bind the socket to address. The socket must not already be bound. The format of ``address`` is: ``(ipv4 address, port)`` .. method:: socket.listen([backlog]) Enable a server to accept connections. If backlog is specified, it must be at least 0 (if it's lower, it will be set to 0); and specifies the number of unaccepted connections tha the system will allow before refusing new connections. If not specified, a default reasonable value is chosen. .. method:: socket.accept() Accept a connection. The socket must be bound to an address and listening for connections. The return value is a pair (conn, address) where conn is a new socket object usable to send and receive data on the connection, and address is the address bound to the socket on the other end of the connection. .. method:: socket.connect(address) Connect to a remote socket at address. The format of address is: ``(ipv4 address, port)`` .. method:: socket.send(bytes) Send data to the socket. The socket must be connected to a remote socket. .. method:: socket.sendall(bytes) Send data to the socket. The socket must be connected to a remote socket. .. method:: socket.recv(bufsize) Receive data from the socket. The return value is a bytes object representing the data received. The maximum amount of data to be received at once is specified by bufsize. .. method:: socket.sendto(bytes, address) Send data to the socket. The socket should not be connected to a remote socket, since the destination socket is specified by address. The ``address`` has the same format as the rest of the methods, see above. .. method:: socket.recvfrom(bufsize) Receive data from the socket. The return value is a pair (bytes, address) where bytes is a bytes object representing the data received and address is the address of the socket sending the data. .. method:: socket.setsockopt(level, optname, value) Set the value of the given socket option. The needed symbolic constants are defined in the socket module (SO_* etc.). The value can be an integer or a bytes-like object representing a buffer. .. method:: socket.settimeout(value) Set a timeout on blocking socket operations. The value argument can be a nonnegative floating point number expressing seconds, or None. If a non-zero value is given, subsequent socket operations will raise a timeout exception if the timeout period value has elapsed before the operation has completed. If zero is given, the socket is put in non-blocking mode. If None is given, the socket is put in blocking mode. .. method:: socket.setblocking(flag) Set blocking or non-blocking mode of the socket: if flag is false, the socket is set to non-blocking, else to blocking mode. This method is a shorthand for certain ``settimeout()`` calls:: sock.setblocking(True) is equivalent to sock.settimeout(None) sock.setblocking(False) is equivalent to sock.settimeout(0.0) .. method:: socket.makefile(mode='rb') Return a file object associated with the socket. The exact returned type depends on the arguments given to makefile(). The support is limited to binary modes only ('rb' and 'wb'). CPython's arguments: ``encoding``, ``errors`` and ``newline`` are not supported. The socket must be in blocking mode; it can have a timeout, but the file object’s internal buffer may end up in a inconsistent state if a timeout occurs. .. note:: **CPython difference:** closing the file object returned by makefile() WILL close the original socket as well. .. method:: socket.read(size) Read up to size bytes from the socket. Return a bytes object. If ``size`` is not given, it behaves just like ``socket.readall()``, see below. .. method:: socket.readall() Read all data available from the socket until ``EOF``. This function will not return until the socket is closed. .. method:: socket.readinto(buf[, nbytes]) Read bytes into the ``buf``. If ``nbytes`` is specified then read at most that many bytes. Otherwise, read at most ``len(buf)`` bytes. Return value: number of bytes read and stored into ``buf``. .. method:: socket.readline() Read a line, ending in a newline character. Return value: the line read. .. method:: socket.write(buf) Write the buffer of bytes to the socket. Return value: number of bytes written. @ 1.1.1.1 log @Import micropython version 1.5.2 into othersrc. Micropython is a python3 implementation that has been optimised for micro-controllers and small embedded systems. It also has a "unix" port. It has an MIT license. This is version 1.5.2 of micropython. MicroPython implements the entire Python 3.4 syntax (including exceptions, "with", "yield from", etc.). The following core datatypes are provided: str (including basic Unicode support), bytes, bytearray, tuple, list, dict, set, frozenset, array.array, collections.namedtuple, classes and instances. Builtin modules include sys, time, and struct. Note that only subset of Python 3.4 functionality implemented for the data types and modules. This is the standard micropython source (version v1.5.2), which has been adapted to use reachover infrastructure. At the present time, libffi (from pkgsrc) is used. Alternative locations for libffi can be set using the PKGSRC_PREFIX definition to make. In the whole scheme of things, micropython is quite small: % size bin/micropython text data bss dec hex filename 393495 1624 2208 397327 6100f bin/micropython % and runs much as expected: % bin/micropython MicroPython v1.5.2 on 2016-01-13; linux version Use Ctrl-D to exit, Ctrl-E for paste mode >>> print("hello world") hello world >>> list(5 * x + y for x in range(10) for y in [4, 2, 1]) [4, 2, 1, 9, 7, 6, 14, 12, 11, 19, 17, 16, 24, 22, 21, 29, 27, 26, 34, 32, 31, 39, 37, 36, 44, 42, 41, 49, 47, 46] >>> % python2.7 Python 2.7.10 (default, Oct 17 2015, 17:55:29) [GCC 4.8.5] on netbsd7 Type "help", "copyright", "credits" or "license" for more information. >>> print("hello world") hello world >>> list(5 * x + y for x in range(10) for y in [4, 2, 1]) [4, 2, 1, 9, 7, 6, 14, 12, 11, 19, 17, 16, 24, 22, 21, 29, 27, 26, 34, 32, 31, 39, 37, 36, 44, 42, 41, 49, 47, 46] >>> ^D % This (reachover) version runs all the tests which the gmake version does. % make t cd bin && make t cd /home/agc/local/micropython/bin/../dist/unix/../tests && env MICROPY_MICROPYTHON=/home/agc/local/micropython/bin/micropython ./run-tests pass basics/0prelim.py pass basics/andor.py pass basics/array1.py pass basics/array_add.py pass basics/array_construct.py pass basics/array_construct2.py ... pass unix/ffi_callback.py pass unix/ffi_float.py pass unix/ffi_float2.py 474 tests performed (15812 individual testcases) 474 tests passed 5 tests skipped: cmd_showbc cmd_verbose machine1 machine_mem extra_coverage % The sources are available from github: https://github.com/micropython/micropython micropython release at: https://github.com/micropython/micropython/releases Sources for 1.5.2 taken from: https://github.com/micropython/micropython/archive/v1.5.2.tar.gz Alistair Crooks agc@@netbsd.org Wed Jan 13 17:15:30 PST 2016 @ text @@