Parsers

Client

httpx
stream = httpx.DuplexStream(
    b'HTTP/1.1 200 OK\r\n'
    b'Content-Length: 23\r\n'
    b'Content-Type: application/json\r\n'
    b'\r\n'
    b'{"msg": "hello, world"}'
)
p = ahttpx.HTTPParser(stream, mode='CLIENT')

# Send the request...
p.send_method_line(b'GET', b'/', b'HTTP/1.1')
p.send_headers([(b'Host', b'www.example.com')])
p.send_body(b'')

# Receive the response...
protocol, code, reason_phase = p.recv_status_line()
headers = p.recv_headers()
body = b''
while buffer := p.recv_body():
    body += buffer

Server

httpx
stream = httpx.DuplexStream(
    b'GET / HTTP/1.1\r\n'
    b'Host: www.example.com\r\n'
    b'\r\n'
)
p = httpx.HTTPParser(stream, mode='SERVER')

# Receive the request...
method, target, protocol = p.recv_method_line()
headers = p.recv_headers()
body = b''
while buffer := p.recv_body():
    body += buffer

# Send the response...
p.send_status_line(b'HTTP/1.1', 200, b'OK')
p.send_headers([
    (b'Content-Length', b'23'),
    (b'Content-Type', b'application/json')
])
p.send_body(b'{"msg": "hello, world"}')
p.send_body(b'')

Connections Low Level Networking