Python 套接字模块.连接到 HTTP 代理,然后对外部资源执行 GET 请求 [英] Python socket module. Connecting to an HTTP proxy then performing a GET request on an external resource
问题描述
首先,我知道还有其他模块(例如请求)更适合且更易于使用,但我想使用套接字模块来更好地理解 HTTP.
To begin with, I understand there are other modules such as Requests that would be better suited and simpler to use, but I want to use the socket module to better understand HTTP.
我有一个执行以下操作的简单脚本:
I have a simple script that does the following:
客户端 ---> HTTP 代理 ---> 外部资源 (GET Google.com)
Client ---> HTTP Proxy ---> External Resource (GET Google.com)
我可以正常连接到 HTTP 代理,但是当我将 google.com 的 GET 请求标头发送到代理时,它根本没有为我提供任何响应.
I am able to connect to the HTTP proxy alright, but when I send the GET request headers for google.com to the proxy, it doesn't serve me any response at all.
#!/usr/bin/python
import socket
import sys
headers = """GET / HTTP/1.1
Host: google.com
"""
socket = socket
host = "165.139.179.225" #proxy server IP
port = 8080 #proxy server port
try:
s = socket.socket()
s.connect((host,port))
s.send(("CONNECT {0}:{1} HTTP/1.1
" + "Host: {2}: {3}
").format(socket.gethostbyname(socket.gethostname()),1000,port,host))
print s.recv(1096)
s.send(headers)
response = s.recv(1096)
print response
s.close()
except socket.error,m:
print str(m)
s.close()
sys.exit(1)
推荐答案
要向代理发出 HTTP 请求,请打开与代理服务器的连接,然后发送 HTTP 代理请求.此请求与普通 HTTP 请求基本相同,但包含绝对 URL 而不是相对 URL,例如
To make a HTTP request to a proxy open a connection to the proxy server and then send a HTTP-proxy request. This request is mostly the same as the normal HTTP request, but contains the absolute URL instead of the relative URL, e.g.
> GET http://www.google.com HTTP/1.1
> Host: www.google.com
> ...
< HTTP response
要使 HTTPS 请求使用 CONNECT 方法打开隧道,然后在该隧道内正常进行,即进行 SSL 握手,然后在隧道内进行正常的非代理请求,例如
To make a HTTPS request open a tunnel using the CONNECT method and then proceed inside this tunnel normally, that is do the SSL handshake and then a normal non-proxy request inside the tunnel, e.g.
> CONNECT www.google.com:443 HTTP/1.1
>
< .. read response to CONNECT request, must be 200 ...
.. establish the TLS connection inside the tunnel
> GET / HTTP/1.1
> Host: www.google.com
这篇关于Python 套接字模块.连接到 HTTP 代理,然后对外部资源执行 GET 请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!