How to send HTTP/1.0 request via urllib2?

后端 未结 2 1747
傲寒
傲寒 2020-12-06 19:13

Seems that urllib2 sends HTTP/1.1 request by default?

2条回答
  •  旧时难觅i
    2020-12-06 19:44

    urllib2 uses httplib under the hood to make the connection. You can change it to http 1.0 as shown below. I've included my apache servers access log to show how the http connection have change to 1.0

    code

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

    access.log

    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"
    

提交回复
热议问题