Python Networking — HTTP, Sockets & APIs

Python AdvancedNetworkingFree Lesson

Advertisement

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

  1. Use requests for HTTP (not raw sockets)
  2. Always set timeout on requests
  3. Use raise_for_status() for error detection
  4. Sessions maintain cookies and connection pooling
  5. Use json= parameter for JSON request bodies

Advertisement

Need Expert Python Help?

Get personalized tutoring, project support, or professional consulting.

Advertisement