Python Network Programming Guide: From Getting Started to Practice

Almost none of today's applications leave the Internet. Whether browsing the web, sending and receiving messages, calling cloud services, or calling between microservices, network programming is a bridge that allows programs on different devices to "talk". This tutorial will use popular language and executable code to help you systematically master the core knowledge of Python network programming.

1. Why do you need to understand network programming?

1.1 The essence of network communication: "process dialogue" across machines

The underlying logic of network programming is very simple - it solves the problem of how processes on different devices exchange data.

  • When you open a browser to access a web page, it is essentially the browser process on your own computer that communicates with the remote web service process.
  • You use chat software to send messages, and the local client process interacts with the chat service process on the server.
  • You call cloud storage to upload files, and the local application process and the cloud storage service process are transmitting data.

In these scenarios, the network connects geographically dispersed processes and allows them to exchange data as if they were on the same machine. This is what network programming is about.

1.2 Layered protocol stack: Why don’t we start writing programs from network cable signals?

The reason why network communication can be achieved with just a few lines of code is because there is a layered protocol stack, and each layer is only responsible for its own work. The four common levels are:

  1. Application layer: HTTP (web page), FTP (file transfer), SMTP (mail), WebSocket (real-time communication), etc. - handle specific business logic.
  2. Transport layer: TCP (reliable transmission), UDP (fast but no guarantee of delivery) - responsible for end-to-end data transmission.
  3. Network layer: IP - Responsible for addressing and routing, delivering data packets from one device to another.
  4. Link layer: Ethernet, Wi-Fi - turns signals into electrical current or radio waves on cables.

Python allows you to program at different levels depending on your needs. You can directly operate TCP/UDP, or you can use it at the application layerrequestsMake an HTTP request, leaving out the low-level details.

2. Python network programming "weapon arsenal"

Python's own standard library already provides powerful network capabilities, and with excellent third-party libraries, it can cover almost all network scenarios.

LibraryHierarchyPurpose
socketTransport layer/network layerDirectly operate TCP/UDP, the most basic network interface
httpApplication layerImplementing a simple HTTP client/server
urllibApplication layerHTTP request library, built into the standard library
requests(Third party)Application layerMore elegant HTTP client, highly recommended
asyncioFrameworkAsynchronous I/O, high-performance network concurrency
websockets(Third party)Application layerWebSocket support, suitable for real-time applications

💡 Tips for library selection: If you want to quickly adjust an API, use it directlyrequests;If you want to make a high-concurrency chat service, learnasyncio;If you want to write a network tool to deeply understand the protocol, thensocketJust the best teacher.

3. Two classic programming models

3.1 Client-server model (C/S)

This is the most familiar pattern: one server waits passively, and multiple clients actively initiate connections.

  • Server: Bind the port, listen for connections, process and respond after receiving the request.
  • Client: Know the address and port of the server, actively initiate a connection, and then send and receive data.

Almost all Internet services are based on this model - web servers, database servers, game servers, etc.

3.2 Peer-to-Peer Model (P2P)

In this model, there is no central server and devices communicate directly with each other. Typical examples include: file sharing (BitTorrent), video calling (WebRTC), and blockchain nodes.

P2P avoids the single-point bottleneck of the server, but it is also more complex to implement and usually needs to deal with issues such as node discovery and NAT penetration.

4. Practical combat: Use Python to operate TCP and UDP

Let’s talk directly in code below. You will find that the two core protocols of network programming - TCP (reliable transmission) and UDP (fast transmission) - actually operate very similarly.

4.1 TCP Programming: Reliable “Telephone Line”

TCP is connection-oriented. You must "dial" the other party before communicating to ensure that data is not lost or out of order during the transmission process. It's like making a phone call: you dial, the other person answers, and then you two talk.

TCP Server: Waits for the client to connect, receives messages and replies.

import socket

# 1. 创建 socket 对象,指定 IPv4 和 TCP
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# 2. 允许端口复用(开发时很有用,防止“地址已占用”)
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

# 3. 绑定本机所有网卡的 8000 端口
server_socket.bind(('0.0.0.0', 8000))

# 4. 开始监听,积压最多 5 个等待的连接
server_socket.listen(5)
print("TCP 服务器已开启,监听 8000 端口...")

try:
    while True:
        # 堵塞等待,直到有客户端连入
        client_socket, addr = server_socket.accept()
        print(f"新连接来自: {addr}")

        # 接收最多 1024 字节数据
        data = client_socket.recv(1024)
        if data:
            print(f"收到: {data.decode()}")

            # 发送响应
            client_socket.send(b"Hello from server!")

        # 通信完毕,关闭该客户连接
        client_socket.close()
finally:
    server_socket.close()

TCP Client: Connect to the server, send a message and wait for a reply.

import socket

client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

try:
    # 连接本地 8000 端口
    client_socket.connect(('localhost', 8000))

    # 发送数据(注意转成字节串)
    client_socket.send(b"Hello from client!")

    # 接收回复
    response = client_socket.recv(1024)
    print(f"服务器回应: {response.decode()}")
finally:
    client_socket.close()

⚠️ Note: TCP is byte stream oriented and has no message boundaries. That is to say, oncesendThe data may be split multiple times on each otherrecv, or possibly multiplesendThe data is stuck together. In actual development, you need to design your own "message delimitation" method (such as line splitting, fixed-length header, etc.).

4.2 UDP Programming: Fast but Unreliable “Postcard”

UDP is connectionless and sends data packets (datagrams) directly. There is no guarantee that the other party will receive it, nor the order. It's like sending a postcard: delivered directly, with no guarantee of delivery or order, but there are no complicated handshakes in the process and it's faster. Suitable for real-time voice, video, online games and other scenarios.

UDP Server:

import socket

server_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
server_socket.bind(('0.0.0.0', 8000))
print("UDP 服务器已开启,监听 8000 端口...")

try:
    while True:
        # 接收数据,同时获得发送方的地址
        data, addr = server_socket.recvfrom(1024)
        print(f"收到来自 {addr} 的消息: {data.decode()}")

        # 直接向该地址回复
        server_socket.sendto(b"UDP response", addr)
finally:
    server_socket.close()

UDP client:

import socket

client_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

try:
    # 无需 connect,直接 sendto
    client_socket.sendto(b"UDP message", ('localhost', 8000))

    # 设置接收超时,避免一直等待
    client_socket.settimeout(2.0)

    try:
        response, addr = client_socket.recvfrom(1024)
        print(f"来自 {addr} 的回应: {response.decode()}")
    except socket.timeout:
        print("请求超时,没有收到回复")
finally:
    client_socket.close()

📌 TCP vs UDP Quick Choice:

  • Requires reliable transmission (files, web pages, databases) → TCP
  • Tolerate a small amount of loss and pursue low latency (voice/video, real-time games) → UDP

5. Advanced skills: high concurrency and HTTP client

When we face tens of thousands of simultaneous connections, the traditional model of one thread serving one client is not enough. Python provides asynchronous programming to deal with this high-concurrency scenario; at the same time, the most commonly used HTTP requests in daily development also have excellent library support.

5.1 Asynchronous network programming (asyncio)

passasyncio, you can handle thousands of connections simultaneously in a single thread, but the code is as clear as synchronization logic.

import asyncio

async def handle_client(reader, writer):
    addr = writer.get_extra_info('peername')
    print(f"新连接: {addr}")

    try:
        while True:
            data = await reader.read(100)   # 异步读取
            if not data:
                break
            message = data.decode()
            print(f"收到 {message}")

            # 异步回写
            response = f"Echo: {message}".encode()
            writer.write(response)
            await writer.drain()            # 确保数据发送完毕
    finally:
        print(f"关闭连接: {addr}")
        writer.close()
        await writer.wait_closed()

async def main():
    server = await asyncio.start_server(handle_client, '127.0.0.1', 8000)
    addrs = ', '.join(str(s.getsockname()) for s in server.sockets)
    print(f"异步服务器运行在: {addrs}")

    async with server:
        await server.serve_forever()

# 启动事件循环
try:
    asyncio.run(main())
except KeyboardInterrupt:
    print("服务器手动停止")

This example implements a simple "Echo" service, but under the hood it can handle a lot of concurrency. existhandle_client, every timeawaitAll will give up control and other coroutines can be executed, thereby achieving high throughput.

5.2 The most elegant HTTP client: requests

Call REST API, crawl web pages, 90% of the work can be usedrequestsDone in a few lines of code.

import requests

BASE_URL = 'https://jsonplaceholder.typicode.com'

def get_posts():
    """获取文章列表"""
    try:
        resp = requests.get(f'{BASE_URL}/posts')
        resp.raise_for_status()                # 如果状态码不是 2xx,抛出异常
        print(f"状态码: {resp.status_code}")
        return resp.json()                     # 自动解码 JSON
    except requests.RequestException as e:
        print(f"GET 请求出错: {e}")
        return None

def create_post(title, body, user_id=1):
    """创建一篇新文章"""
    payload = {'title': title, 'body': body, 'userId': user_id}
    try:
        resp = requests.post(f'{BASE_URL}/posts', json=payload)
        resp.raise_for_status()
        print(f"创建成功,状态码: {resp.status_code}")
        return resp.json()
    except requests.RequestException as e:
        print(f"POST 请求出错: {e}")
        return None

if __name__ == '__main__':
    posts = get_posts()
    if posts:
        print(f"获取到 {len(posts)} 篇文章")

    new_post = create_post("Python 网络编程", "用 requests 库真是太方便了")
    if new_post:
        print(f"新文章 ID: {new_post['id']}")
  • raise_for_status()is a good practice to catch 4xx/5xx errors immediately.
  • resp.json()Automatically parse the response body into a Python dictionary.
  • json=payloadparameter letrequestsHelp you serialize and set the correct Content-Type.

6. Network security: a bottom line that cannot be ignored

There are thousands of things to do in network programming, but safety comes first. Here are some security practices that must be considered:

  1. Encrypted transmission Be sure to use TLS/SSL in production environments. In Python it can besocketWrap one layerssl.SSLContext, or use directlyhttps

  2. Input verification All data from the network should be considered untrustworthy, and the length, type, and content must be strictly verified to prevent attacks such as SQL injection and command injection.

  3. Authentication and Authorization Determine who is visiting and what he can do. JWT and OAuth2 are commonly used solutions.

  4. Rate Limiting To prevent certain clients from spamming requests and causing service suspension, current limiting can be implemented at the application layer or gateway layer.

  5. Security Error Handling The information returned to the client must mask internal stack details to avoid leaking sensitive information such as server paths and dependency versions.

  6. Update dependencies in a timely manner Network libraries are also software, and loopholes are constantly being discovered. Regularly update Python versions and third-party libraries to fix known vulnerabilities.

🛡️ Experience: Don’t wait until the project is attacked before considering security. Develop the habit of using HTTPS and verifying input parameters from the beginning.

Technology evolves faster than tutorials update. Here are a few current mainstream or emerging directions to help you grasp the key points of subsequent learning:

  • RESTful API: Still the mainstream design style of Web service interfaces, based on HTTP verbs + resources.
  • WebSocket: Full duplex, long connection, suitable for chatting, online games, and real-time data push.
  • gRPC: A high-performance RPC framework launched by Google, based on HTTP/2 and Protocol Buffers, suitable for strongly typed communication between microservices.
  • MQTT: lightweight publish/subscribe protocol, widely used in the Internet of Things (IoT) field.
  • QUIC / HTTP/3: The next generation transmission protocol based on UDP has been adopted by browsers and large services, and will gradually replace some TCP+TLS scenarios in the future.

Which technology to choose depends on your scenario: if you just provide data to the front end, REST or WebSocket is enough; if it is a low-power device with massive connections, consider MQTT; if it is an internal service call, gRPC is usually a good choice.

8. Summary

Python network programming has many levels - from the bottomsocketto the upper levelrequestsasyncio, you can choose the appropriate level of abstraction according to your needs. The core points of this tutorial can be condensed into these few sentences:

  • Understand the model first: C/S or P2P? This determines your program role.
  • Protocol selection based on scenario: TCP for reliable transmission, UDP for low latency.
  • Master key libraries:socketbase,requestsGet HTTP done,asyncioDeal with high concurrency.
  • Security always comes first: encryption, verification, and current limiting, none of them can be missing.

Network programming is the basic skill for building distributed systems, microservices, and real-time applications. I hope this article can help you lay a solid foundation so that you can be confident when facing various network needs.