Python Networking — HTTP, Sockets & APIs
Networking is essential for web scraping, API consumption, and building network applications.
Learning Objectives
- Make HTTP requests with the requests library
- Understand socket programming basics
- Build API clients with proper authentication
- Handle network errors gracefully
HTTP with requests
import requests
# GET request
response = requests.get('https://api.github.com/users/python')
print(response.status_code) # 200
print(response.json()) # Parsed JSON
# POST request
data = {'username': 'alice', 'password': 'secret'}
response = requests.post('https://httpbin.org/post', json=data)
# Headers and params
headers = {'Authorization': 'Bearer token123'}
params = {'q': 'python', 'per_page': 10}
response = requests.get(
'https://api.github.com/search/repositories',
headers=headers,
params=params
)
Error Handling
import requests
from requests.exceptions import HTTPError, ConnectionError, Timeout
try:
response = requests.get('https://api.example.com/data', timeout=5)
response.raise_for_status()
data = response.json()
except HTTPError as e:
print(f"HTTP error: {e}")
except ConnectionError:
print("Connection failed")
except Timeout:
print("Request timed out")
Session and Cookies
import requests
with requests.Session() as s:
s.post('https://example.com/login', data={'user': 'alice', 'pass': '123'})
response = s.get('https://example.com/dashboard')
Socket Programming
import socket
# Simple TCP client
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect(('example.com', 80))
client.send(b'GET / HTTP/1.1\r\nHost: example.com\r\n\r\n')
response = client.recv(4096)
print(response.decode())
client.close()
# Better: use context manager
with socket.create_connection(('example.com', 80)) as sock:
sock.sendall(b'GET / HTTP/1.1\r\nHost: example.com\r\n\r\n')
data = sock.recv(4096)
Key Takeaways
- Use
requestsfor HTTP (not raw sockets) - Always set
timeouton requests - Use
raise_for_status()for error detection - Sessions maintain cookies and connection pooling
- Use
json=parameter for JSON request bodies