Skip to main content
Archived

This project has been archived by its maintainers, and is no longer receiving any updates.

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.

Release files for urllib3-ext-hface 0.3.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for urllib3-ext-hface 0.3.4
File Size Uploaded
urllib3_ext_hface-0.3.4.tar.gz 22.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for urllib3-ext-hface 0.3.4
File Interpreter ABI Platform
urllib3_ext_hface-0.3.4-py3-none-any.whl Python 3 none any Details

Total release size: 52.7 kB

Release files / urllib3_ext_hface-0.3.4.tar.gz

Download URL urllib3_ext_hface-0.3.4.tar.gz
Size 22.7 kB
Tags Source
SHA-256 checksum
How to use checksums
172634bc3b73b02fb9f34ba653c623a99e2b7526fd0bc912e3a837d8e1b4c16e
BLAKE2b-256 checksum
How to use checksums
8bcc0949733abbb6d329e78754877a2cea22435bb21616a843ca11a535324780
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/4.0.1 CPython/3.11.4

Release files / urllib3_ext_hface-0.3.4-py3-none-any.whl

Download URL urllib3_ext_hface-0.3.4-py3-none-any.whl
Size 30.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ea12a8d0bcafd57d19af4251134adb80595e3631d1faa576ba2677a87d5b1d92
BLAKE2b-256 checksum
How to use checksums
f71ca1d421fb3e144514aec5a8cfdb147856766004bf650810e9aac3fa572a83
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/4.0.1 CPython/3.11.4
Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page