6

似乎 urllib2 默认发送 HTTP/1.1 请求?

4

2 回答 2

6

为了避免猴子补丁httplib(全局更改),您可以子类化HTTPConnection并定义自己的 http 处理程序:

#!/usr/bin/env python
try:
    from httplib import HTTPConnection
    from urllib2 import HTTPHandler, build_opener
except ImportError: # Python 3
    from http.client import HTTPConnection
    from urllib.request import HTTPHandler, build_opener

class HTTP10Connection(HTTPConnection):
    _http_vsn = 10
    _http_vsn_str = "HTTP/1.0" 

class HTTP10Handler(HTTPHandler):
    def http_open(self, req):
        return self.do_open(HTTP10Connection, req)

opener = build_opener(HTTP10Handler)
print(opener.open('http://stackoverflow.com/q/13656757').read()[:100])
于 2014-04-13T10:48:02.387 回答
5

urllib2 在后台使用 httplib 进行连接。您可以将其更改为 http 1.0,如下所示。我已经包含了我的 apache 服务器访问日志,以显示 http 连接如何更改为 1.0

代码

import urllib2, httplib
httplib.HTTPConnection._http_vsn = 10
httplib.HTTPConnection._http_vsn_str = 'HTTP/1.0'
print urllib2.urlopen('http://localhost/').read()

访问日志

127.0.0.1 - - [01/Dec/2012:09:10:27 +0300] "GET / HTTP/1.1" 200 454 "-" "Python-urllib/2.7"
127.0.0.1 - - [01/Dec/2012:09:16:32 +0300] "GET / HTTP/1.0" 200 454 "-" "Python-urllib/2.7"
于 2012-12-01T06:19:09.193 回答