socket – TCP, UDP and RAW socket support

Warning

This module is disabled in 6.x and will removed in 7.x. Please use networking libraries instead. (Native networking will provide a socket compatible class.)

Create TCP, UDP and RAW sockets for communicating over the Internet.

class socket.socket(family: int, type: int, proto: int)

Create a new socket

Parameters:
  • family (int) – AF_INET or AF_INET6
  • type (int) – SOCK_STREAM, SOCK_DGRAM or SOCK_RAW
  • proto (int) – IPPROTO_TCP, IPPROTO_UDP or IPPROTO_RAW (ignored)
AF_INET :int
AF_INET6 :int
SOCK_STREAM :int
SOCK_DGRAM :int
SOCK_RAW :int
bind(self, address: Tuple[str, int])

Bind a socket to an address

Parameters:address (tuple(str, int)) – tuple of (remote_address, remote_port)
listen(self, backlog: int)

Set socket to listen for incoming connections

Parameters:backlog (int) – length of backlog queue for waiting connetions
accept(self)

Accept a connection on a listening socket of type SOCK_STREAM, creating a new socket of type SOCK_STREAM. Returns a tuple of (new_socket, remote_address)

connect(self, address: Tuple[str, int])

Connect a socket to a remote address

Parameters:address (tuple(str, int)) – tuple of (remote_address, remote_port)
send(self, bytes: ReadableBuffer)

Send some bytes to the connected remote address. Suits sockets of type SOCK_STREAM

Parameters:bytes (ReadableBuffer) – some bytes to send
recv_into(self, buffer: WriteableBuffer, bufsize: int)

Reads some bytes from the connected remote address, writing into the provided buffer. If bufsize <= len(buffer) is given, a maximum of bufsize bytes will be read into the buffer. If no valid value is given for bufsize, the default is the length of the given buffer.

Suits sockets of type SOCK_STREAM Returns an int of number of bytes read.

Parameters:
  • buffer (WriteableBuffer) – buffer to receive into
  • bufsize (int) – optionally, a maximum number of bytes to read.
recv(self, bufsize: int)

Reads some bytes from the connected remote address. Suits sockets of type SOCK_STREAM Returns a bytes() of length <= bufsize

Parameters:bufsize (int) – maximum number of bytes to receive
sendto(self, bytes: ReadableBuffer, address: Tuple[str, int])

Send some bytes to a specific address. Suits sockets of type SOCK_DGRAM

Parameters:
recvfrom(self, bufsize: int)

Reads some bytes from the connected remote address. Suits sockets of type SOCK_STREAM

Returns a tuple containing * a bytes() of length <= bufsize * a remote_address, which is a tuple of ip address and port number

Parameters:bufsize (int) – maximum number of bytes to receive
setsockopt(self, level: int, optname: int, value: int)

Sets socket options

settimeout(self, value: int)

Set the timeout value for this socket.

Parameters:value (int) – timeout in seconds. 0 means non-blocking. None means block indefinitely.
setblocking(self, flag: bool)

Set the blocking behaviour of this socket.

Parameters:flag (bool) – False means non-blocking, True means block indefinitely.
socket.getaddrinfo(host: str, port: int) → Tuple[int, int, int, str, str]

Gets the address information for a hostname and port

Returns the appropriate family, socket type, socket protocol and address information to call socket.socket() and socket.connect() with, as a tuple.