4
votes
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"

2
from the documentation: Requests allow you to send HTTP/1.1 requests. why would you want do specify the protocol version? let requests worry about these things. - hiro protagonist
@hiroprotagonist I want to crawl a website. But the website analyze most parameters of http request header and the http protocol version to avoid been crawl by robot. Only when useing http 1.0 to access the website, it will return right result. - maston
@maston i used a variable that i called headers to pass a User-Agent string, like this: 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'} - Rocky Raccoon

2 Answers

1
votes

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.

0
votes

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 OK\r\n'))

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.