1

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))
0xHades
  • 31
  • 1
  • 4
  • 1
    HTTP/3 uses [QUIC](https://en.wikipedia.org/wiki/QUIC), which is its own protocol. You should instead use an existing implementation, just search for "quic python". – Marc Sep 13 '20 at 04:42
  • You need the speed for Instagram? What time critical data do they offer? – Klaus D. Sep 13 '20 at 04:43
  • You are basically trying to paint a horse red and then call it a Ferrari race car. That's not how HTTP/3 works vs. HTTP/1. It is a completely different thing and much more complex. And things do not get magically faster just by using UDP, there is much more involved to actually gain performance than just switching from TCP to UDP. – Steffen Ullrich Sep 13 '20 at 05:46

2 Answers2

1

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.

Dmitri
  • 392
  • 3
  • 10
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-svc header.

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.