In-depth Python network programming: from basics to practice

Python, as a widely used high-level programming language, has many advantages, one of which is its network programming capabilities. Python's powerful network libraries such as socket, requests, urllib, asyncio, etc., make it excellent in network programming. This article will discuss in depth the application of Python in network programming, including basic socket programming, to advanced asynchronous IO network programming, and how we can make full use of these tools to develop network applications.

Socket programming basics

Socket is the cornerstone of network programming. Python's socket module provides a set of simple APIs that can help us quickly establish network connections. Let's start with a simple example of communication between a server and a client:

Service-Terminal:

import socket

# 创建 socket 对象
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# 绑定端口
s.bind(('localhost', 12345))

# 设置最大连接数,超过后排队
s.listen(5) 

while True:
    # 建立客户端连接
    c, addr = s.accept()      
    print ('Got connection from', addr)
    
    # 发送数据
    c.send('Thank you for connecting'.encode())
    
    # 关闭连接
    c.close()  

client:

import socket              

# 创建 socket 对象
s = socket.socket()         

# 连接到服务器
s.connect(('localhost', 12345))

# 接收数据
print (s.recv(1024).decode())
s.close()                    

Running the server-side code followed by the client-side code gives the following output:

Got connection from ('127.0.0.1', 51768)
Thank you for connecting

Here, the TCP protocol is used for communication. TCP is a connection-oriented protocol, which ensures stable communication between two computers.

Socket Programming Fundamentals and Its Practice

As mentioned above, Socket is the cornerstone of network programming. Python's socket module provides a set of simple APIs to help us quickly establish network connections. Let's take a deeper look at socket programming with a chat room application:

# 这是一个简单的命令行聊天室服务器

import socket
import select

server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind(('localhost', 12345))
server_socket.listen(10)
inputs = [server_socket]

while inputs:
    rs, _, _ = select.select(inputs, [], [])

    for r in rs:
        if r is server_socket:
            client_socket, _ = r.accept()
            inputs.append(client_socket)
        else:
            msg = r.recv(1024)
            if not msg:
                inputs.remove(r)
            else:
                for socket in inputs:
                    if socket is not server_socket:
                        socket.send(msg)

This server uses the select module to handle multiple socket connections simultaneously. When a new client connects, it adds the new socket to the input list. When the server receives a message, it forwards the message to all other clients.

HTTP network request

In Python, we often use the requests library to make HTTP requests. Its use is very intuitive, let's look at a simple example:

import requests

# 发起 GET 请求
response = requests.get('https://www.example.com')

# 输出响应的文本信息
print(response.text)

HTTP network request and its advanced usage

In Python, we often use the requests library to make HTTP requests. However, in addition to the basic GET and POST requests, the requests library also supports more advanced functions, such as session objects, cookie handling, and proxy settings.

For example, we can use the session object to maintain a session, which is very useful on websites that require login to access:

import requests

s = requests.Session()

# 先进行登录
s.post('http://httpbin.org/post', data = {
    
    'key':'value'})

# 然后访问需要登录后才能看到的页面
r = s.get('http://httpbin.org/cookies')
print(r.text)

Asynchronous IO network programming

Python's asyncio library provides us with asynchronous IO capabilities, allowing us to achieve more efficient IO operations in network programming. Here is an example of a simple asynchronous network request:

import asyncio
import aiohttp

async def fetch(session, url):
    async with session.get(url) as response:
        return await response.text()

async def main():
    async with aiohttp.ClientSession() as session:
        html = await fetch(session, 'http://python.org')
        print(html)

loop = asyncio.get_event_loop()
loop.run_until_complete(main())

Web scraping
Python's web programming capabilities also make it an excellent tool for web scraping. For example, we can use the BeautifulSoup library to parse HTML and grab the information we need:

import requests
from bs4 import BeautifulSoup

# 发起请求
r = requests.get('http://example.com')

# 解析HTML
soup = BeautifulSoup(r.text, 'html.parser')

# 抓取所有的链接
links = [a['href'] for a in soup.find_all('a', href=True)]
print(links)

web development

Python also excels in web development. For example, we can use the Flask library to quickly develop a web application:

from flask import Flask, render_template

app = Flask(__name__)

@app.route("/")
def hello():
    return render_template('index.html')

if __name__ == "__main__":
    app.run()

Flask allows us to quickly create a web application, and it also supports basic functions of web development such as routing, template rendering, and static files.

FTP service

Did you know that Python can also act as an FTP server? pyftpdlib is a powerful, highly configurable FTP server library. It supports most commands of FTP, and since it is written in pure Python, it can run on any platform supported by Python.

One More Thing…

Finally, let me share a useful but little-known trick: using Python's http.server module

Block quickly creates a simple web server. Just enter the following command on the command line:

python -m http.server

This command will start a simple HTTP server on your machine on port 8000 by default. This is useful for quickly sharing files or doing some simple web development.

The above is the basic knowledge and some advanced usage of Python in network programming. I hope this information can be helpful to you.

insert image description here

Guess you like

Origin blog.csdn.net/weixin_44816664/article/details/131382507