Network#
Short network snippets the operator pastes into a shell or
notebook. urllib and socket ship with Python; httpx
is the recommended third-party HTTP client.
GET a URL and read the body as text (stdlib).
import urllib.request
body = urllib.request.urlopen("https://example.com/").read().decode()
GET with httpx (async-capable, modern API).
import httpx
r = httpx.get("https://example.com/", timeout=5.0)
r.raise_for_status()
body = r.text
POST a JSON body.
r = httpx.post("https://api.example/items",
json={"name": "x"}, timeout=5.0)
Stream a large download to disk.
with httpx.stream("GET", url) as r, open("dump.bin", "wb") as f:
for chunk in r.iter_bytes():
f.write(chunk)
Open a TCP connection with a deadline.
import socket
sock = socket.create_connection(("example.com", 443), timeout=3)
Quick TCP port probe (returns True if open).
import socket
def is_open(host: str, port: int, timeout: float = 1.0) -> bool:
try:
with socket.create_connection((host, port), timeout=timeout):
return True
except OSError:
return False
Resolve a hostname.
addrs = socket.gethostbyname_ex("example.com")[2]
Reverse-resolve an IP.
name, *_ = socket.gethostbyaddr("8.8.8.8")
CIDR membership test with ipaddress.
import ipaddress
if ipaddress.ip_address("10.0.0.42") in ipaddress.ip_network("10.0.0.0/8"):
internal()
Enumerate every host in a network.
for ip in ipaddress.ip_network("192.168.1.0/29").hosts():
check(ip)
Parse a URL.
from urllib.parse import urlparse
u = urlparse("https://op:pw@example.com:8443/path?x=1#f")
u.scheme, u.hostname, u.port, u.path, u.query, u.fragment
Build a query string from a dict.
from urllib.parse import urlencode
qs = urlencode({"q": "search term", "page": 2})
References#
Snippets for the snippets catalogue.
Networking for the deeper sockets / HTTP / SSH surface.