'How can implement http/3 protocol throuh udp (DTLS) socket in python?
I read that http/3 uses UDP instead of TCP to send requests, so that makes it faster, And I really need the speed of http/3, So what can I do, to implement it in python?. I wrote this code based on my understanding of the protocol:
It's a hypertext protocol, You using UDP instead of TCP, change the http/1.1 in the packet to http/3, send it.
And I think I'm wrong.
here's the code I wrote:
import socket
from OpenSSL import SSL # for DTLS
connection = 'close' # or keep-alive
protocol = 'HTTP/3' # or HTTP/1.1
packet = f'GET / {protocol}\r\nHost: i.instagram.com\r\nConnection: {connection}\r\n\r\n'
def callback(conn, cert, errnum, depth, ok): cert.get_subject(); return ok
# Initialize context
ctx = SSL.Context(SSL.TLSv1_2_METHOD)
ctx.set_verify(SSL.VERIFY_PEER, callback) # Demand a certificate
# Set up client
client = SSL.Connection(ctx, socket.socket(socket.AF_INET, socket.SOCK_DGRAM))
addr = ('i.instagram.com', 443) #using DTLS
client.connect(addr)
buffer = packet.encode()
client.sendall(buffer) # it stuck here
print(sock.recv(4096))
Solution 1:[1]
A more simple approach might be to use a Python ASGI web server that supports HTTP/3.
I've created an example project here using the hypercorn ASGI web server.
There appears to be two ways that a server can request HTTP/3;
- with alpn
- through the
alt-svcheader.
The hypercorn server uses the header approach.
I'm using Ubuntu 20.04 LTS and the only browser I can find that supports HTTP/3 using the alt-svc header as of 2020-12-05 is the FireFox nightly build.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Rob Blackbourn |
