HTTP 2 request in python 2.7
Asked Answered
A

3

9

Is there any difference in making request to HTTP/1 and HTTP/2 in python.

I can make HTTP/1.x calls in python like

url = 'http://someURL'
values = {'param1' : 'key',
          'param2' : 'key2'}
data = urllib.urlencode(values)
print data
req = urllib2.Request(url, data)
response = urllib2.urlopen(req)
the_page = response.read()
print the_page

Is python supporting making HTTP/2 by default or should I add anything extra.

Anagnos answered 2/1, 2016 at 21:38 Comment(6)
I'm almost sure it uses HTTP2, and you can check the docs for that. But I think you're better off using the requests library. (pip install requests). They say that urllib2 is "thoroughly broken..."Powel
FYI: hyper.readthedocs.org/en/latestVarese
@PadraicCunningham That's the authors of requests.Powel
@PythonGuy from the docs, I couldn't find that requests supports HTTP/2 also authors of hyper says requests not supports HTTP/2Anagnos
@Anagnos Are you sure? Check again. But you can always do a raw socket send in a TCP packet (yes, you can!) and read the server output using http 2.0.Powel
You could use pycurl but it's verbose and not beginner friendly, especially if you don't know libcurl.Enharmonic
R
4

As others mentioned in the comments to the question the requests library does not support HTTP/2.

From the requests library documentation:

Requests allows you to send organic, grass-fed HTTP/1.1 requests, without the need for manual labor.

As of now the only HTTP/2 client for Python I know of is hyper, which quoting from the docs:

supports Python 3.4 and Python 2.7.9, and can speak HTTP/2 and HTTP/1.1

Rattat answered 19/8, 2016 at 14:34 Comment(1)
hyper is no longer maintainedEnharmonic
A
9

For reference, as of 2019, another library supporting HTTP/2 is HTTPX.

HTTPX is a fully featured HTTP client for Python 3, which provides sync and async APIs, and support for both HTTP/1.1 and HTTP/2.

This requires at least Python 3.6. However, at the time of writing in 2020, Python 2 is already EOL, so Python 3.6 should be okay for any users.

Anesthesiology answered 18/2, 2020 at 7:52 Comment(0)
R
4

As others mentioned in the comments to the question the requests library does not support HTTP/2.

From the requests library documentation:

Requests allows you to send organic, grass-fed HTTP/1.1 requests, without the need for manual labor.

As of now the only HTTP/2 client for Python I know of is hyper, which quoting from the docs:

supports Python 3.4 and Python 2.7.9, and can speak HTTP/2 and HTTP/1.1

Rattat answered 19/8, 2016 at 14:34 Comment(1)
hyper is no longer maintainedEnharmonic
E
0

Use hyper with requests module.

import requests
from hyper.contrib import HTTP20Adapter
s = requests.Session()
s.mount('https://http2bin.org', HTTP20Adapter())
r = s.get('https://http2bin.org/get')
print(r.status_code)

https://hyper.readthedocs.io/en/latest/quickstart.html#requests-integration

Encomiastic answered 7/7, 2020 at 17:40 Comment(1)
hyper is no longer maintainedEnharmonic

© 2022 - 2024 — McMap. All rights reserved.