Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python-requests how to send cipher name/http2

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!

like image 604
Biplov Avatar asked Feb 12 '21 06:02

Biplov


People also ask

Does Python requests support HTTP 2?

No, it doesn't.

Does Python requests use TLS?

Requests uses the Python standard library ssl module under the hood - this supports various versions of SSL and TLS.

How do I change TLS version in Python?

PROTOCOL_TLS" will do the job for you like it should try to connect on all tls versions, so if your remote server only support TLSv1. 2 even if your python is compiled with TLSv1. 3 it will use TLSv1. 2 and will connect as expected (Downgrading to TLS1.

Does requests use https?

Requests verifies SSL certificates for HTTPS requests, just like a web browser. SSL Certificates are small data files that digitally bind a cryptographic key to an organization's details.


1 Answers

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.

like image 60
CodeIt Avatar answered Nov 15 '22 00:11

CodeIt