显然,当通过s发送时,http请求结果格式不正确

2024-09-19 23:28:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在处理套接字操作,并用python编写了一个基本的拦截代理。它工作正常,但有些主机返回400个错误的请求响应。在

不过,这些请求看起来没有格式错误。这里有一个:

GET http://www.baltour.it/ HTTP/1.1
Host: www.baltour.it
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:28.0) Gecko/20100101 Firefox/28.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: keep-alive

相同的请求,原始:

^{pr2}$

我用来发送请求的代码是最基本的socket操作(虽然我不认为问题出在那里,但它在大多数主机上都能正常工作)

socket_client.send(request_raw)

while插座_客户端.recv用于获取响应(但在这里没有问题,响应的格式良好,尽管其状态为400)。在

有什么想法吗?在


Tags: httphost代理getapplicationwww格式错误
1条回答
网友
1楼 · 发布于 2024-09-19 23:28:29

不与代理对话时,不应将http://hostname部分放在HTTP头中;请参见section 5.1.2 of the HTTP 1.1 RFC 2616 spec

The most common form of Request-URI is that used to identify a resource on an origin server or gateway. In this case the absolute path of the URI MUST be transmitted (see section 3.2.1, abs_path) as the Request-URI, and the network location of the URI (authority) MUST be transmitted in a Host header field.

(emphasis mine);abs_path是请求URI的绝对路径部分,而不是完整的绝对URI本身。在

例如,服务器希望您发送:

GET / HTTP/1.1
Host: www.baltour.it

但是,接收服务器应该能够容忍不正确的行为。服务器似乎也违反了RFC。在同一节中,它进一步写道:

To allow for transition to absoluteURIs in all requests in future versions of HTTP, all HTTP/1.1 servers MUST accept the absoluteURI form in requests, even though HTTP/1.1 clients will only generate them in requests to proxies.

相关问题 更多 >