Python Central HubSign in

Python Programming / Python Networking

Networking in Python

Learn networking basics (IP, ports, TCP/UDP, HTTP) and how Python programs communicate over a network.

diagram what the requests library is doing that a raw socket is not mermaid
The two ends of this picture are the same connection. A socket gives you bytes; everything above it -- finding the host, negotiating TLS, framing an HTTP message, decoding the body -- is work someone has to do. Writing it yourself is the reason to reach for a library, not a weakness of sockets.

What is networking?

Networking is how computers exchange data.

Key concepts:

  • IP address: identifies a machine
  • Port: identifies a program/service on that machine
  • Protocol: rules for communication

Common protocols:

  • TCP: reliable (web, SSH)
  • UDP: fast, not guaranteed (streaming, DNS)
  • HTTP/HTTPS: web protocol (built on TCP)

What you’ll learn here

  • Make HTTP requests (APIs) using requests and urllib
  • Work with JSON data
  • Build a basic HTTP server
  • Use sockets for low-level TCP communication
  • Timeouts, retries, and error handling

Safety note

Always be careful when handling:

  • user input from the internet
  • authentication tokens
  • opening ports on public machines
sketch Everything requests does above a raw socket p5.js
Both ends of this picture are the same connection. A socket gives you an ordered stream of bytes and nothing else -- finding the host, negotiating TLS, framing an HTTP message and decoding the body are all work somebody has to do. Writing that yourself is the reason to reach for a library, not a shortcoming of sockets.

How this section is organised

This page is the map for the networking lessons. Work through them in this order and each one builds on the last:

  • Requests and APIs: fetch data over HTTP with urllib and requests, then read the JSON that comes back.
  • Building a Simple HTTP Server: the other side of the conversation, serving responses with http.server.
  • Sockets: the low-level TCP layer that HTTP itself is built on.
  • Networking Errors and Timeouts: what to do when the network misbehaves.

What happens when you open a web page

Seeing the layers together makes everything later easier. When your code calls requests.get("https://example.com/data"), these steps happen in order:

  1. DNS turns the name example.com into an IP address.
  2. A TCP connection is opened to that address on a port (443 for HTTPS, 80 for HTTP).
  3. For HTTPS, a TLS handshake encrypts the connection.
  4. Your program writes an HTTP request, and the server writes back a status, headers and a body.

Each step can fail on its own, which is why errors and timeouts get a lesson of their own.

IP, port and protocol in practice

An IP address finds the machine and a port finds the program on it, much like a building address and a flat number. 127.0.0.1 (also called localhost) always means "this computer", which makes it perfect for practising without any real network. Python's standard library can show you both:

Python
import socket
 
# Ask the operating system for the address of the local machine name
print(socket.gethostbyname("localhost"))  # 127.0.0.1
 
# Well-known ports
print(socket.getservbyname("http"))   # 80
print(socket.getservbyname("https"))  # 443

Common misunderstandings

  • "TCP sends messages." It sends a stream of bytes. Two sends may arrive as one read, or one send as two reads, so protocols must mark where messages end.
  • "UDP is just a worse TCP." It is deliberately lightweight: no connection and no delivery guarantee, which suits live video, games and DNS lookups.
  • "HTTPS is a different network." It is HTTP carried inside TLS over the same TCP.

Where to start

If you only want to consume an API, go straight to requests and JSON. If you want to understand how it works underneath, continue with sockets after the HTTP server lesson.

Check yourself

quizCheck yourself
  1. What does a TCP socket actually guarantee?

    Show answer

    B — Bytes arrive in order and intact, with no message boundaries — Measured elsewhere on this site: `sendall(b'HELLO')` then `sendall(b'WORLD')` read by `recv(4)` came back as `[b'HELL', b'O', b'WORL']`. Framing is the application's job.

  2. Which of these does a raw socket NOT do for you?

    Show answer

    C — Parse HTTP headers — Ordering, retransmission and checksums are TCP's job. Everything protocol-shaped above that — HTTP framing, TLS, content decoding — is written on top.

  3. What is the first thing `requests.get('https://host/path')` must do?

    Show answer

    B — Resolve the hostname to an IP address — You cannot connect to a name. DNS resolution happens first, then the TCP connection, then the TLS handshake, and only then does any HTTP get written.

  4. Why use a `Session` for repeated calls to the same host?

    Show answer

    B — It reuses the connection and shares headers and cookies — Reusing the connection skips the TCP and TLS handshakes on every subsequent request, which is usually the dominant cost for small responses.

🧪 Try It Yourself

Exercise 1 – HTTP GET with urllib

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 2 – Socket Connection

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 3 – Resolve a Hostname

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Was this page helpful?

Let us know how we did