Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python 3.x how to get http version (using requests library)

Preferably using the requests library

HTTP/1.1 200 OK        <-- I want this...
Content-Type: text/html; charset=utf-8

That property doesn't seem to be provided http://docs.python-requests.org/en/master/api/#requests.Response

Is there a way to access the raw response string?

I found http://docs.python-requests.org/en/master/user/quickstart/#raw-response-content but I'm not seeing any content

r = requests.head(uri, stream=True)
print(r.raw.read(10)) # -> b''
like image 916
user1267259 Avatar asked May 03 '16 19:05

user1267259


People also ask

Which Python library would you use to serve HTTP requests?

In this article, you will learn about the Python Requests library, which allows you to send HTTP requests in Python.

Does requests work with Python 3?

The Requests library is available for both Python 2 and Python 3 from the Python Package Index (PyPI), and has the following features: Allows you to send HTTP/1.1 PUT, DELETE, HEAD, GET and OPTIONS requests with ease.

Which Python module is used for interacting with HTTP protocol?

urllib. urllib is a module built into the Python standard library and uses http. client which implements the client side of HTTP and HTTPS protocols.

What is HTTP request library?

The requests library is the de facto standard for making HTTP requests in Python. It abstracts the complexities of making requests behind a beautiful, simple API so that you can focus on interacting with services and consuming data in your application.

What is the requests library in Python?

The requests library is the de facto standard for making HTTP requests in Python. It abstracts the complexities of making requests behind a beautiful, simple API so that you can focus on interacting with services and consuming data in your application.

How do I install requests in Python?

To reiterate, Requests is a Python library. The good news is that there are a few ways to install the Requests library. To see the full list of options at your disposal, you can view the official install documentation for Requests here. You can make use of pip, easy_install, or tarball.

What is urllib request Python?

Source code: Lib/urllib/request.py The urllib.request module defines functions and classes which help in opening URLs (mostly HTTP) in a complex world — basic and digest authentication, redirections, cookies and more. The Requests package is recommended for a higher-level HTTP client interface.

What is the protocol used for requests in Python3?

Requests for python3 uses urllib3. Urllib3's class urllib3.response.HTTPResponse is backwards compatible with httplib’s HTTPResponse see urllib3 docs Now if it's backwards compatible you have to check documentation for httplib and if you search you'll find HTTP protocol version used by server. 10 for HTTP/1.0, 11 for HTTP/1.1.


1 Answers

I think what you want is this. Calling version on raw will give you HTTP version. (I found the example of server running HTTP 1.0 using Shodan for testing purposes)

>>> import requests
>>> response = requests.get("http://104.71.136.252/", timeout=60, verify=False)
>>> response.raw.version
10
>>> response = requests.get("http://stackoverflow.com", timeout=60, verify=False)
>>> response.raw.version
11

This is not mentioned in the docs directly, I found it by using PyCharm's autocomplete feature. But I have looked into it. The reason why is HTTP version returned as integer is historical.

Requests for python3 uses urllib3. Urllib3's class urllib3.response.HTTPResponse is backwards compatible with httplib’s HTTPResponse see urllib3 docs

Now if it's backwards compatible you have to check documentation for httplib and if you search you'll find

HTTP protocol version used by server. 10 for HTTP/1.0, 11 for HTTP/1.1.

Here is the exact link HTTPResponse.version

like image 111
J91321 Avatar answered Sep 28 '22 00:09

J91321