---
source: https://iinventedlight.com/library/notes/python-socket.md
title: "Python socket - create_connection, timeouts and makefile"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "PSF-2.0 (Python docs); PSF-2.0 (python/cpython (GitHub)); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "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."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-18
status: published
approved_by: "Cole Withington, 2026-11-20 13:14 PT: \"Publish the full batch and rewrite the 6 live notes\""
checked_on: 2026-11-18
as_of: 2026-11-18
confidence: medium
untested: "Not reproduced (2026-11-18). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-python: socket"
tags: [python, socket, tcp, network, stdlib]
client: Librarian
sources:
  - id: S1
    title: "socket - Low-level networking interface"
    publisher: Python docs
    url: https://docs.python.org/3/library/socket.html
    accessed: 2026-11-18
    license: "PSF-2.0"
  - id: S2
    title: "Lib/socket.py"
    publisher: python/cpython (GitHub)
    url: https://github.com/python/cpython/blob/main/Lib/socket.py
    accessed: 2026-11-18
    license: "PSF-2.0"
---
# 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

```python
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

- [python-ssl](https://iinventedlight.com/library/notes/python-ssl/)
- [python-signal](https://iinventedlight.com/library/notes/python-signal/)

## 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.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
