How to define the HTTP protocol version in requests?
Question:
import requests
requests.get("http://www.sample.com")
How to modifiy the parameter to send the requests like below:
“GET www.sample.com HTTP/1.0”
“GET www.sample.com HTTP/1.1”
Answers:
Requests does not support sending HTTP/1.0 messages. It is hard to understand why you’d need to do that: HTTP/1.1 was originally specified in RFC 2616, published in June 1999. HTTP/1.0 has therefore been obsolete for more than 16 years: modern tools largely do not support HTTP/1.0 any longer.
Try to pass a User-Agent string, like this:
import requests
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36'}
URL = 'http://serveraddress'
page = requests.get(URL, headers=headers)
print(page.text)
Background: Working with a legacy Shoutcast streaming audio server, I was getting the following error:
requests.exceptions.ConnectionError: ('Connection aborted.', BadStatusLine('ICY 200 OKrn'))
I noticed, too, that wget
attempted to stream the data, even when given an -O
output directive.
Comparing with Developer Tools in Chrome, I could see several request headers that the browser set, including User-Agent
. I copied the User-Agent
value from Chrome and used it as a parameter in a requests.get
function.
You have to mokey patch the code by setting http.client.HTTPConnection
‘s _http_vsn_str
property to 'HTTP/X.Y'
before making your request:
import requests
from http.client import HTTPConnection
HTTPConnection._http_vsn_str = 'HTTP/1.0'
requests.get('http://example.com')
import requests
requests.get("http://www.sample.com")
How to modifiy the parameter to send the requests like below:
“GET www.sample.com HTTP/1.0”
“GET www.sample.com HTTP/1.1”
Requests does not support sending HTTP/1.0 messages. It is hard to understand why you’d need to do that: HTTP/1.1 was originally specified in RFC 2616, published in June 1999. HTTP/1.0 has therefore been obsolete for more than 16 years: modern tools largely do not support HTTP/1.0 any longer.
Try to pass a User-Agent string, like this:
import requests
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36'}
URL = 'http://serveraddress'
page = requests.get(URL, headers=headers)
print(page.text)
Background: Working with a legacy Shoutcast streaming audio server, I was getting the following error:
requests.exceptions.ConnectionError: ('Connection aborted.', BadStatusLine('ICY 200 OKrn'))
I noticed, too, that wget
attempted to stream the data, even when given an -O
output directive.
Comparing with Developer Tools in Chrome, I could see several request headers that the browser set, including User-Agent
. I copied the User-Agent
value from Chrome and used it as a parameter in a requests.get
function.
You have to mokey patch the code by setting http.client.HTTPConnection
‘s _http_vsn_str
property to 'HTTP/X.Y'
before making your request:
import requests
from http.client import HTTPConnection
HTTPConnection._http_vsn_str = 'HTTP/1.0'
requests.get('http://example.com')