Python socket - create_connection, timeouts and makefile
Answer: socket.create_connection() connects to a TCP service. This module provides socket operations and some related functions. Call settimeout to bound blocking socket operations.
Examples
import socket
with socket.create_connection(("example.com", 80), timeout=5.0) as sock:
sock.sendall(b"HEAD / HTTP/1.1\r\nHost: example.com\r\n\r\n")
print(sock.recv(1024))
# server
with socket.socket() as server:
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server.bind(("127.0.0.1", 0))
server.listen()
host, port = server.getsockname()
Rules
- On Unix, the module supports IP and Unix domain sockets. On other systems it supports IP.
- Functions specific for a socket are available as methods of the socket object.
See also
Claims
- id: C1 claim: create_connection connects to a TCP service. source: S1 evidence: "Connect to a TCP service"
- id: C2 claim: The module provides socket operations and related functions. source: S2 evidence: "This module provides socket operations and some related functions"
- id: C3 claim: Socket-specific functions are methods on the socket object. source: S2 evidence: "Functions specific for a socket are available as methods of the socket object"
Sources
- [S1] socket - Low-level networking interface. Python docs. https://docs.python.org/3/library/socket.html . License: PSF-2.0. Accessed 2026-11-18.
- [S2] Lib/socket.py. python/cpython (GitHub). https://github.com/python/cpython/blob/main/Lib/socket.py . License: PSF-2.0. Accessed 2026-11-18.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Python docs, socket - Low-level networking interface; python/cpython (GitHub), Lib/socket.py. PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation. Text was condensed and changed from the originals.
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Python docs, socket - Low-level networking interface; python/cpython (GitHub), Lib/socket.py. PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation.
Cite this
iinventedlight. 2026. "Python socket - create_connection, timeouts and makefile." iinventedlight Library, iil:note:vsjyf18n, version of 2026-11-18. https://iinventedlight.com/library/notes/python-socket/. License: CC-BY-4.0.
@misc{iil_note_vsjyf18n,
author = {{iinventedlight}},
title = {{Python socket - create\_connection, timeouts and makefile}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-18},
url = {https://iinventedlight.com/library/notes/python-socket/},
note = {Permanent ID iil:note:vsjyf18n. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/python-socket.md.