Python:运行 HTTP 代理服务器时如何获取 URL?
我正在创建一个 HTTP 代理服务器,它能够检索用户请求的网站的 URL。我的 HTTP 代理服务器只能使用单个文件(我不能有多个文件)。
我能够在无限运行的 while 循环中检测连接和地址并接收来自客户端的消息:
while True:
conn, addr = created_socket.accept()
data_received = conn.recv(1024)
print(data_received)
当我在指定端口上运行服务器并在 Chrome 中输入 [IP 地址]:[端口号] 时,我得到打印 data_received 后的结果如下:
b'GET /www.google.com HTTP/1.1\r\nHost: 192.168.1.2:5050\r\nConnection: keep-alive\r\n升级不安全请求:1\r\n用户代理:Mozilla/5.0(Windows NT 10.0;Win64;x64)AppleWebKit/537.36(KHTML,如 Gecko)Chrome/98.0.4758.102 Safari/537.36\ r\n接受:文本/html,应用程序/xhtml+xml,应用程序/xml;q=0.9,图像/avif,图像/webp,图像/apng,*/*;q=0.8,应用程序/签名交换;v=b3;q= 0.9\r\nAccept-Encoding: gzip, deflate\r\nAccept-Language: en-US,en;q=0.9\r\n\r\n
有没有系统的方法我可以检索哪个 URL(在本例中为 www.google.com
)?现在,我正在为 conn.recv
(1024
) 的恒定缓冲区大小进行编码。但是,我想知道是否有一种方法可以首先检索客户端的消息大小,将其存储在变量中,然后将该变量传递给 recv
的缓冲区大小参数?
I am creating an HTTP Proxy Server that is able to retrieve the URL of the website requested by a user. I am only allowed to use a single file for my HTTP Proxy Server (I can't have multiple files).
I am able within a infinite running while loop to detect a connection and the address and receive a message from the client:
while True:
conn, addr = created_socket.accept()
data_received = conn.recv(1024)
print(data_received)
When I run my server on a specified port and type the [IP Address]:[Port Number] into Chrome, I get the following result after printing data_received:
b'GET /www.google.com HTTP/1.1\r\nHost: 192.168.1.2:5050\r\nConnection: keep-alive\r\nUpgrade-Insecure-Requests: 1\r\nUser-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36\r\nAccept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9\r\nAccept-Encoding: gzip, deflate\r\nAccept-Language: en-US,en;q=0.9\r\n\r\n
Is there a systematic way in which I can retrieve the URL (in this case, www.google.com
)? Right now, I am coding in a constant buffer size for conn.recv
(1024
). However, I was wondering if there was first a way to first retrieve the message size of the client, store it in a variable, and then pass that variable to the buffer size parameter for recv
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论