10
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"

4
  • 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. Commented Jul 30, 2015 at 17:57
  • related: How to send HTTP/1.0 request via urllib2? Commented Jul 30, 2015 at 19:19
  • 1
    @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. Commented Jul 31, 2015 at 0:18
  • @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'} Commented Jul 8, 2021 at 18:34

3 Answers 3

9

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')
Sign up to request clarification or add additional context in comments.

Comments

-2

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.

2 Comments

"largely do not support HTTP/1.0" -- you still can send even http 0.9 requests in some cases
@Lukasa Normally really don't need to consider the version of the HTTP protocol, but sometimes website do not want been crawled. So I have to make the HTTP requests fit for the require of the website to get the right returns. The website I want to crawl only return rightly on the HTTP 1.0. Thanks for your help.
-3

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.