pythonsocketssslopensslhttp3

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

  • One can most certainly implement HTTP/3 in Python. It has already been done: check out aioquic.

    Also, please have a look at the latest set of QUIC and HTTP/3 Internet Drafts. Your naive implementation is based on wrong assumptions.