Skip to main content

urllib3 extension to support HTTP/1.1, HTTP/2 and HTTP/3 independently of Python httplib

Project description

PyPI

This project is a fork of the akamai/hface project but highly slimmed. The purpose of that project is to enable basic support for HTTP/1.1, HTTP/2 and HTTP/3 in urllib3.

  • HTTP/1.1, HTTP/2, and HTTP/3 support through respectively h11, h2 and aioquic

  • Sans-IO core with pluggable protocol implementations

  • Layered design with well-defined APIs

  • Client-oriented only

Documentation

This library is pretty straight forward and is very easy to understand.

from urllib3_ext_hface import HTTPProtocolFactory, HTTP1Protocol, HTTP2Protocol, HTTP3Protocol

protocol = HTTPProtocolFactory.new(HTTP2Protocol)

# ...
protocol.bytes_to_send()  # Out to your network I/O implementation
# ...
protocol.bytes_received()  # Whatever comes from your network I/O implementation
# ...
protocol.next_event()  # Output the next event in order that the state-machine generated

Here protocol is either of type HTTPOverQUICProtocol or HTTPOverTCPProtocol, depending on the foremost, you will either be on a DGRAM (UDP) or STREAM (TCP) socket.

Find just bellow a somewhat stupid example that can help you get started.

from urllib3_ext_hface import HTTPProtocolFactory, HTTP1Protocol, HTTP2Protocol, HTTP3Protocol
import ssl
import socket
import certifi

RECV_LENGTH = 4096

if __name__ == "__main__":

    ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
    ctx.set_alpn_protocols(["h2"])

    ctx.load_default_certs()
    ctx.load_verify_locations(certifi.where())

    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    sock = ctx.wrap_socket(sock, server_hostname="httpbin.org")

    sock.connect(("httpbin.org", 443))

    sock.settimeout(3)

    protocol = HTTPProtocolFactory.new(HTTP2Protocol)

    # Start HTTP2*PRI
    while True:
        data_out = protocol.bytes_to_send()

        if not data_out:
            break

        sock.sendall(data_out)

        protocol.bytes_received(
            sock.recv(RECV_LENGTH)
        )

    stream_id = protocol.get_available_stream_id()

    protocol.submit_headers(
        stream_id,
        [
            (b":authority", b"httpbin.org"),
            (b":scheme", b"https"),
            (b":path", b"/headers"),
            (b":method", b"GET"),
            (b"User-Agent", b"Awesome Sans-IO!")
        ],
        True
    )

    sock.sendall(
        protocol.bytes_to_send()
    )

    while True:

        try:
            protocol.bytes_received(
                sock.recv(RECV_LENGTH)
            )
        except TimeoutError:
            protocol.connection_lost()

        event = protocol.next_event()

        if hasattr(event, "data"):
            print(event.data)

        print(event)

        if hasattr(event, "end_stream") and event.end_stream is True:
            break

    protocol.submit_close()

    sock.sendall(protocol.bytes_to_send())

    sock.close()

License

Copyright 2022 Akamai Technologies, Inc

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

urllib3_ext_hface-0.3.4.tar.gz (22.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

urllib3_ext_hface-0.3.4-py3-none-any.whl (30.0 kB view details)

Uploaded Python 3

File details

Details for the file urllib3_ext_hface-0.3.4.tar.gz.

File metadata

  • Download URL: urllib3_ext_hface-0.3.4.tar.gz
  • Upload date:
  • Size: 22.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.1 CPython/3.11.4

File hashes

Hashes for urllib3_ext_hface-0.3.4.tar.gz
Algorithm Hash digest
SHA256 172634bc3b73b02fb9f34ba653c623a99e2b7526fd0bc912e3a837d8e1b4c16e
MD5 359b6c3da44f67674de7aecc3fd0c6b5
BLAKE2b-256 8bcc0949733abbb6d329e78754877a2cea22435bb21616a843ca11a535324780

See more details on using hashes here.

File details

Details for the file urllib3_ext_hface-0.3.4-py3-none-any.whl.

File metadata

File hashes

Hashes for urllib3_ext_hface-0.3.4-py3-none-any.whl
Algorithm Hash digest
SHA256 ea12a8d0bcafd57d19af4251134adb80595e3631d1faa576ba2677a87d5b1d92
MD5 2725491c1db7d987cfc054b80e40601d
BLAKE2b-256 f71ca1d421fb3e144514aec5a8cfdb147856766004bf650810e9aac3fa572a83

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page