Detailed tutorial on implementing proxy server in 50 lines of Python code

A proxy server is an intermediary server between the client and the target server that sends requests on behalf of the client and returns responses to the client. By building our own proxy server, we can implement functions such as request interception, modification, and forwarding. This article will introduce how to implement a proxy server using 50 lines of Python code.

Insert image description here

1. Preparation work

Before we begin, we need to make sure we have the following libraries installed:

  • socket: used for creating sockets and network communication.
  • threading: used to implement multi-threading processing.
  • urllib.parse: used to parse URLs.

It can be installed using the pip command, for example:

pip install socket threading urllib

2. Code to implement proxy server

Below is a simple Python code example that demonstrates how to implement a proxy server.

import socket
import threading
import urllib.parse
def handle_client(client_socket):
    """
    处理客户端请求
    """
    # 接收客户端请求数据
    request_data = client_socket.recv(1024)
    # 解析请求报文
    request_lines = request_data.decode().split("\r\n")
    # 获取请求方法、URL和协议版本
    method, url, protocol = request_lines[0].split()
    # 解析URL
    url_parts = urllib.parse.urlparse(url)
    hostname = url_parts.netloc
    
    # 创建与目标服务器的连接
    server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server_socket.connect((hostname, 80))
    # 发送客户端请求数据给目标服务器
    server_socket.sendall(request_data)
    # 接收目标服务器响应数据并发送给客户端
    while True:
        response_data = server_socket.recv(1024)
        if response_data:
            client_socket.send(response_data)
        else:
            break
    # 关闭连接
    client_socket.close()
    server_socket.close()
def main():
    # 创建代理服务器套接字
    server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
     # 设置地址和端口
    server_address = ("127.0.0.1", 8888)
    # 绑定地址和端口
    server_socket.bind(server_address)
    # 监听连接
    server_socket.listen(5)
    print("代理服务器已启动,监听地址:%s:%d" % server_address)
    while True:
        # 等待客户端连接
        client_socket, client_address = server_socket.accept()
        # 创建线程处理客户端请求
        client_thread = threading.Thread(target=handle_client, args=(client_socket,))
        client_thread.start()
if __name__ == "__main__":
    main()

In the above code, we first define a handle_clientfunction named to handle client requests. In this function, we first receive the request data sent by the client, then parse the request message and extract the request method, URL and protocol version. Next, we parse the URL to get the hostname of the target server. Then, create a connection to the target server and send the client request data to the target server. Next, we loop to receive the response data from the target server and send it to the client. Finally, we close the connection.

In mainthe function, we create a proxy server socket and set the address and port. Then, we bind the address and port and start listening for connections. In an infinite loop, we wait for client connections and for each client connection we create a new thread to handle the request.

3. Use proxy server

To use the proxy server we built, you can specify the proxy server address and port by modifying the browser's proxy settings. The specific operations are as follows:

1. Open the settings of your browser (such as Chrome).

2. Find the proxy settings in network settings or advanced settings.

3. Set the proxy to the address and port of the proxy server we built (for example, 127.0.0.1:8888).

4. Save the settings and restart the browser.

At this point, when the browser sends a request, the request will be forwarded and processed through our proxy server.

With these 50 lines of Python code, we successfully implemented a simple proxy server. By building our own proxy server, we can implement functions such as request interception, modification, and forwarding, which is very useful for web crawlers and data collection. I hope the content shared in this article can help everyone and successfully build their own proxy server.

Guess you like

Origin blog.csdn.net/weixin_44617651/article/details/133123095