all Python 3socketterm
socket
module
Low-level BSD socket networking: TCP and UDP connections, addresses, name resolution.
Syntax
import socket
socket is the ground floor of networking: raw bytes over TCP or UDP with no protocol on top. For HTTP use urllib.request or http.client, for encryption wrap the socket with ssl, and for many connections at once use asyncio or selectors. The main trap: recv may return fewer bytes than requested, so read in a loop, and send with sendall rather than send.
Examples
import socket
a, b = socket.socketpair()
a.sendall(b'ping')
print(b.recv(4)) # → b'ping'
a.close(); b.close()
See also
Common errors
Open in the full glossarydocs.python.orgReport an inaccuracy