为什么我会收到“HTTP/1.1 400 Bad Request”执行时出错?
所以我想(通过代理)向网站发送请求。脚本看起来像这样,它是用 python 中的套接字库制作的:
import socket
TargetDomainName="www.stackoverflow.com"
TargetIP="151.101.65.69"
TargetPort=80
ProxiesIP=["107.151.182.247"]
ProxiesPort=[80]
Connect=f"CONNECT {TargetDomainName} HTTP/1.1"
Connection=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
Connection.connect((ProxiesIP[0],ProxiesPort[0]))
Connection.sendto(str.encode(Connect),(TargetIP, TargetPort))
Connection.sendto(("GET /" + TargetIP + " HTTP/1.1\r\n").encode('ascii'), (TargetIP, TargetPort))
Connection.sendto(("Host: " + ProxiesIP[0] + "\r\n\r\n").encode('ascii'), (TargetIP, TargetPort))
print (Connection.recv(1028))
Connection.close()
我的问题是为什么我收到 400 bad request 错误?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您没有说明400回复是来自代理还是目标服务器。但是您的两个命令都畸形。
您的
连接命令缺少端口号,
host
标题,因为您请求http 1.1,并且落后线路断开以正确终止命令。您的
get 命令发送到目标服务器(如果
连接
成功),不应通过IP地址请求资源。它还为host
标头发送错误的值。该命令是相对于目标服务器的,因此需要指定目标服务器的主机名。另外,您应该使用
send()
/sendall()
而不是sendto()
。尝试更多类似的东西:
您确实需要在发送
get
命令之前读取代理的回复。代理将发送其自己的HTTP答复,指示它是否成功连接到目标服务器。您确实不应该手动实施HTTP,有很多HTTP库可以为您处理这些详细信息。 Python甚至有一个内置:
You did not indicate whether the 400 reply is coming from the proxy or the target server. But both of your commands are malformed.
Your
CONNECT
command is missing a port number, aHost
header since you are requesting HTTP 1.1, and trailing line breaks to terminate the command properly.Your
GET
command is sent to the target server (ifCONNECT
is successful) and should not be requesting a resource by IP address. It is also sending the wrong value for theHost
header. The command is relative to the target server, so it needs to specify the target server's host name.Also, you should be using
send()
/sendall()
instead ofsendto()
.Try something more like this instead:
You really need to read the proxy's reply before sending the
GET
command. The proxy will send its own HTTP reply indicating whether it successfully connected to the target server or not.You really should not be implementing HTTP manually though, there are plenty of HTTP libraries for Python that can handle these details for you. Python even has one built-in: http.client