5

I am trying to replicate the following client requests via python-requests.

enter image description here Under client connection I see HTTP Version which is 2.0 and TLS version which is 1.3, as up to my knowledge I know that requests utilizes TLS 1.3. My requests are failing as of now.

And I wonder if I need to pass certificates. I would like to understand how this request is different from regular request which would be simply as

r = requests.get('someurl')

How can I use requests to use the exact client connection show in requests? I don't fully understand each pointer, How would I use h2 ALPN/ with that specific cipher name? I am not expecting an solid answer to the question rather an explanation would be much more helpful!

Biplov
  • 1,136
  • 1
  • 20
  • 44

2 Answers2

8

python-requests doesn't support HTTP 2 request. You can use httpx package.

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.

Example


import ssl
import httpx

# create an ssl context
ssl_context = ssl.SSLContext(protocol=ssl.PROTOCOL_TLS)
# ssl.PROTOCOL_TLS - Selects the highest protocol version that both the client and server support.
# Despite the name, this option can select both "SSL" and "TLS" protocols.

# set protocol to use
ssl_context.set_alpn_protocols(["h2"])

CIPHERS = 'ECDH+AESGCM:ECDH+CHACHA20:DH+AESGCM:DH+CHACHA20:ECDH+AES256:DH+AES256:ECDH+AES128:DH+AES:ECDH+HIGH:DH+HIGH:RSA+AESGCM:RSA+AES:RSA+HIGH:!aNULL:!eNULL:!MD5:!3DES'

# set ciphers
ssl_context.set_ciphers(CIPHERS)

# httpx verify param lets you pass a standard library ssl.SSLContext
response  = httpx.get('https://example.com', verify=ssl_context)

print(response.http_version)
# outputs HTTP/2

Instead of using ssl.SSLContext, you can also use httpx.create_ssl_context() to set the ssl context.

CodeIt
  • 3,492
  • 3
  • 26
  • 37
0

As far as I know python-requests is a library which currently1 doesn't support HTTP/2.0. This question has been answered here.

However there are python libraries like Python httpx supporting HTTP/2.0!

Kind regards,


1 Feb 16, 2021

Raphael Medaer
  • 2,528
  • 12
  • 18