我正在尝试通过 python-requests 复制以下客户端请求。
在客户端连接下,我看到 HTTP 版本为 2.0,TLS 版本为 1.3,据我所知,我知道请求使用 TLS 1.3。到目前为止,我的请求都失败了。
我想知道我是否需要通过证书。我想了解此请求与常规请求有何不同,常规请求就像
r = requests.get('someurl')
如何使用请求来使用请求中显示的确切客户端连接?我不完全理解每个指针,我将如何使用带有特定密码名称的 h2 ALPN/?我并不期待这个问题得到一个可靠的答案,而是一个解释会更有帮助!
最佳答案
python-requests
不支持 HTTP 2 要求。您可以使用 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.
示例
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 usehttpx.create_ssl_context()
to set the ssl context.
关于python-requests 如何发送密码名/http2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66167183/