How to send an HTTP / 1.0 request through urllib2?

Does urllib2 seem to send an HTTP / 1.1 request by default?

+6
source share
2 answers

urllib2 uses httplib under the hood to establish a connection. You can change it to http 1.0 as shown below. I turned on the apache server access log to show how the HTTP connection changed to 1.0

the 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" 
+4
source

To avoid httplib monkey interception (global change), you can subclass HTTPConnection and define your own http handler:

 #!/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]) 
+4
source

All Articles