← All notes

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

See also

Claims

Sources

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

Permanent ID: iil:note:vsjyf18n

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.},
}

Markdown

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.