This module provides access to the BSD socket interface.
See corresponding CPython module for comparison.
Functions below which expect a network address, accept it in the format of (ipv4_address, port), where ipv4_address is a string with dot-notation numeric IPv4 address, e.g. 8.8.8.8, and port is integer port number in the range 1-65535. Note the domain names are not accepted as ipv4_address, they should be resolved first using socket.getaddrinfo().
(ipv4_address, port)
ipv4_address
8.8.8.8
socket.getaddrinfo()
Create a new socket using the given address family, socket type and protocol number. The initialiser takes the following arguments: Family type:
socket.AF_INET
socket.AF_LORA
socket.AF_SIGFOX
socket.SOCK_STREAM
socket.SOCK_DGRAM
socket.SOCK_RAW
socket.IPPROTO_IP
socket.IPPROTO_ICMP
socket.IPPROTO_UDP
socket.IPPROTO_TCP
socket.IPPROTO_IPV6
socket.IP_ADD_MEMBERSHIP
socket.IPPROTO_RAW
By default, sockets are set to be blocking.
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. This function takes the following arguments:
SO_*
level
socket.SOL_SOCKET
socket.SOL_LORA
socket.SOL_SIGFOX
optname
socket.SO_REUSEADDR
socket.SO_CONFIRMED
socket.SO_DR
socket.SO_RX
socket.SO_TX_REPEAT
socket.SO_OOB
socket.SO_BIT
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:
(family, type, proto, canonname, sockaddr)
s = socket.socket() s.connect(socket.getaddrinfo('www.micropython.org', 80)[0][-1])
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.
close()
Bind the socket to address. The socket must not already be bound. The address parameter must be a tuple containing the IP address and the port.
socket
address
In the case of LoRa sockets, the address parameter is simply an integer with the port number, for instance: s.bind(1)
s.bind(1)
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 that the system will allow before refusing new connections. If not specified, a default reasonable value is chosen.
Accept a connection. The socket must be bound to an address and listening for connections. The return value is a pair (conn, address) where connis 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.
(conn, address)
conn
Connect to a remote socket at address.
Send data to the socket. The socket must be connected to a remote socket.
Alias of s.send(bytes).
s.send(bytes)
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.
bufsize
Send data to the socket. The socket should not be connected to a remote socket, since the destination socket is specified by address.
Receive data from the socket. The return value is a pair (bytes, address) where bytes is a bytes object representing the data received and addressis the address of the socket sending the data.
(bytes, address)
bytes
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.
None
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 will override the timeout setting
This method is a shorthand for certain settimeout() calls:
settimeout()
s.setblocking(True) # is equivalent to s.settimeout(None) s.setblocking(False) #is equivalent to s.settimeout(0.0)
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.
rb
wb
encoding
errors
newline
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.
Difference to CPython Closing the file object returned by makefile() WILL close the original socket as well.
makefile()
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.
size
socket.readall()
Read all data available from the socket until EOF. This function will not return until the socket is closed.
Read bytes into the buf. If nbytes is specified then read at most that many bytes. Otherwise, read at most len(buf) bytes.
buf
nbytes
len(buf)
Return value: number of bytes read and stored into buf.
Read a line, ending in a newline character.
Return value: the line read.
Write the buffer of bytes to the socket.
Return value: number of bytes written.
Perform the SSL handshake on the previously “wrapped” socket with ssl.wrap_socket(). could be used when the socket is non-blocking and the SSL handshake is not performed during connect().
Example:
from network import WLAN import time import socket import ssl import uselect as select wlan = WLAN(mode=WLAN.STA) wlan.connect(ssid='', auth=(WLAN.WPA2, '')) while not wlan.isconnected(): time.sleep(1) print("Wifi .. Connecting") print ("Wifi Connected") a = socket.getaddrinfo('www.postman-echo.com', 443)[0][-1] s = socket.socket() s.setblocking(False) s = ssl.wrap_socket(s) try: s.connect(a) except OSError as e: if str(e) == '119': # For non-Blocking sockets 119 is EINPROGRESS print("In Progress") else: raise e poller = select.poll() poller.register(s, select.POLLOUT | select.POLLIN) while True: res = poller.poll(1000) if res: if res[0][1] & select.POLLOUT: print("Doing Handshake") s.do_handshake() print("Handshake Done") s.send(b"GET / HTTP/1.0\r\n\r\n") poller.modify(s,select.POLLIN) continue if res[0][1] & select.POLLIN: print(s.recv(4092)) break break
When no arguments are passed this function returns the configured DNS servers Primary (Index=0) and backup (Index = 1) to set primary and Backup DNS servers specify the Index and Ip Address.
>>> socket.dnsserver() ('10.0.0.1', '8.8.8.8')
Setting DNS servers:
>>> socket.dnsserver(1, '0.0.0.0') >>> socket.dnsserver() ('10.0.0.1', '0.0.0.0')
socket.error, socket.timeout
socket.error
socket.timeout