How to implement network programming of Python's underlying technology
Network programming is an important technical field in modern software development. Through network programming, we can realize the connection between applications communication to achieve cross-machine and cross-platform data transmission and interaction. As a widely used programming language, Python provides simple and powerful underlying technology to implement network programming. This article will introduce how to use Python's underlying technology for network programming and provide some specific code examples.
import socket # 创建套接字对象 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # 绑定地址和端口 s.bind(('127.0.0.1', 8000)) # 监听连接 s.listen(5) # 等待客户端连接 client_socket, addr = s.accept() print(f"连接已建立,客户端地址:{addr}") # 接收数据 data = client_socket.recv(1024) # 发送数据 client_socket.send(b"Hello, client!") # 关闭连接 client_socket.close() s.close()
The above code first creates a socket object and then binds an address and port. Then listen for connections and wait for client connections. After the client connects successfully, it can receive and send data, and finally close the connection.
import select import socket # 创建套接字对象 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # 绑定地址和端口 s.bind(('127.0.0.1', 8000)) # 监听连接 s.listen(5) # 保存所有客户端套接字的列表 inputs = [s] while True: # 获取可读的套接字 readable, _, _ = select.select(inputs, [], []) for sock in readable: # 如果是主服务器套接字,则表示有新的连接 if sock == s: client_socket, addr = s.accept() print(f"连接已建立,客户端地址:{addr}") inputs.append(client_socket) # 否则为客户端套接字,表示有数据发送 else: data = sock.recv(1024) if data: print(f"收到客户端发送的数据:{data.decode()}") sock.send(b"Hello, client!") else: print("客户端已断开连接") sock.close() inputs.remove(sock)
The above code uses the select module to listen to readable sockets. If the readable socket is the main server socket s, it indicates a new connection request. If it is another client socket, it means that data has been sent and the response can be received and sent. If empty data is received, the client has disconnected.
import socket import threading def handle_client(client_socket, addr): print(f"连接已建立,客户端地址:{addr}") while True: data = client_socket.recv(1024) if data: print(f"收到客户端发送的数据:{data.decode()}") client_socket.send(b"Hello, client!") else: print("客户端已断开连接") client_socket.close() break # 创建套接字对象 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # 绑定地址和端口 s.bind(('127.0.0.1', 8000)) # 监听连接 s.listen(5) while True: # 等待客户端连接 client_socket, addr = s.accept() # 创建一个新的线程处理客户端连接 t = threading.Thread(target=handle_client, args=(client_socket, addr)) t.start()
The above code mainly handles each client connection by creating a new thread. Each thread has an independent socket object that can receive data sent by the client and send responses. At the same time, the main thread is still listening for new connection requests.
Through the above examples, we can see that Python provides a complete set of underlying technologies to implement network programming. Using these underlying technologies, we can flexibly build various network applications and achieve efficient communication and interaction.
The above is the detailed content of How to implement network programming with Python's underlying technology. For more information, please follow other related articles on the PHP Chinese website!