Skip to main content

A modern HTTP client for Python with HTTP/3, WebSocket extensions, and enhanced security features

Project description

urllib4: An Enhanced HTTP Client for Python

Project Status Python Versions Development Stage

ai-urllib4 is a modern HTTP client for Python that builds upon the foundation of urllib3 while adding enhancements for modern web applications. It provides a powerful yet user-friendly interface for making HTTP requests with advanced features.

Features

urllib4 provides a comprehensive set of features for modern web applications:

✅ Core Features:

  • Connection pooling and thread safety
  • Basic URL parsing and manipulation
  • HTTP header handling and collections
  • Multipart form data encoding
  • SSL/TLS utility functions
  • File upload functionality
  • HTTP/HTTPS request handling
  • Proxy support
  • Retry mechanisms
  • Redirect handling
  • Compression support

✅ Advanced Features:

  • Enhanced HTTP/2 Support
  • WebSocket capabilities with extensions and subprotocols
  • Improved security features
  • HTTP/3 (QUIC) support with Multipath QUIC

Usage Example

You can use urllib4 for your HTTP requests with a simple, intuitive API:

# Simple GET request
>>> import urllib4
>>> resp = urllib4.request("GET", "http://httpbin.org/robots.txt")
>>> resp.status
200
>>> resp.data
b"User-agent: *\nDisallow: /deny\n"

# POST request with JSON data
>>> import urllib4
>>> import json
>>> data = {"name": "John", "age": 30}
>>> resp = urllib4.request(
...     "POST",
...     "http://httpbin.org/post",
...     headers={"Content-Type": "application/json"},
...     body=json.dumps(data).encode()
... )
>>> resp.status
200

Installation

You can install urllib4 with pip:

$ pip install urllib4

For additional features, you can install optional dependencies:

# For HTTP/3 support
$ pip install urllib4[http3]

# For WebSocket subprotocols
$ pip install urllib4[websocket]

# For all optional features
$ pip install urllib4[all]

Alternatively, you can install from source:

$ git clone https://github.com/zinzied/urllib4.git
$ cd urllib4
$ pip install -e .

Development and Testing

To set up a development environment:

$ git clone https://github.com/zinzied/urllib4.git
$ cd urllib4
$ pip install -e ".[dev]"

To run tests:

$ python -m pytest

Note that many tests are currently failing as the library is under active development.

Documentation

Documentation is currently limited to code comments and this README. As the project matures, more comprehensive documentation will be developed.

Roadmap

The following features are planned for future development:

HTTP/3 Enhancements

  • Connection migration for improved reliability
  • HTTP/3 priority management
  • WebTransport support

WebSocket Enhancements

  • Additional WebSocket extensions
  • WebSocket over HTTP/2

HTTP/2 Support

import urllib4
from urllib4.http2 import inject_into_urllib4, ConnectionProfile

# Enable HTTP/2 support
inject_into_urllib4()

# Create a pool manager with a specific connection profile
http = urllib4.PoolManager(http2_profile=ConnectionProfile.HIGH_PERFORMANCE)

# Make a request (automatically uses HTTP/2 if the server supports it)
response = http.request("GET", "https://nghttp2.org")
print(f"HTTP version: {response.version_string}")

WebSocket Support

from urllib4.websocket import WebSocketConnection

# Connect to a WebSocket server with compression and health monitoring
ws = WebSocketConnection(
    "wss://echo.websocket.org",
    enable_compression=True,
    compression_level=9,
    enable_health_monitoring=True,
    ping_interval=30.0,
    ping_timeout=5.0,
)

# Connect to the server
ws.connect()

# Send a text message
ws.send("Hello, WebSocket!")

# Send a binary message
ws.send(b"\x01\x02\x03\x04")

# Send a structured object using a subprotocol
ws = WebSocketConnection(
    "wss://echo.websocket.org",
    protocols=["json"],
)
ws.connect()
ws.send({"name": "John", "age": 30})  # Automatically encoded as JSON

# Receive a message with timeout
try:
    message = ws.receive(timeout=5.0)
    if isinstance(message, dict):
        print(f"Received JSON: {message}")
    else:
        print(f"Received: {message.text}")
except WebSocketTimeoutError:
    print("Timed out waiting for message")

# Close the connection
ws.close()

WebSocket Extensions and Subprotocols

urllib4 supports various WebSocket extensions and subprotocols:

  • Extensions:

    • permessage-deflate: Compresses WebSocket messages for better performance
  • Subprotocols:

    • json: Automatically encodes/decodes JSON data
    • msgpack: Efficiently encodes/decodes MessagePack data (requires msgpack package)
    • cbor: Efficiently encodes/decodes CBOR data (requires cbor2 package)

HTTP/3 Support

import urllib4
from urllib4.http3 import HTTP3Connection, HTTP3Settings, QUICSettings, inject_into_urllib4

# Direct HTTP/3 usage
quic_settings = QUICSettings(
    enable_multipath=True,  # Enable Multipath QUIC
    max_paths=4,
    enable_0rtt=True,       # Enable 0-RTT for faster connections
)

http3_settings = HTTP3Settings(
    quic=quic_settings,
)

# Create an HTTP/3 connection
conn = HTTP3Connection(
    "cloudflare-quic.com",  # A server that supports HTTP/3
    settings=http3_settings,
)

# Connect and make a request
conn.connect()
response = conn.request("GET", "/")
print(f"Status: {response.status}")
print(f"Body: {response.data.decode()[:100]}...")

# Close the connection
conn.close()

# Alternatively, inject HTTP/3 support into urllib4
inject_into_urllib4()

# Now all HTTPS requests will automatically use HTTP/3 when available
http = urllib4.PoolManager()
response = http.request("GET", "https://cloudflare-quic.com/")

Enhanced Security Features (Planned)

# This is a planned API - not yet implemented
import urllib4
from urllib4.util.cert_verification import SPKIPinningVerifier, CertificateTransparencyPolicy
from urllib4.util.hsts import HSTSCache, HSTSHandler

# Create a pool manager with SPKI pinning
pins = {
    "example.com": {"pin-sha256:YLh1dUR9y6Kja30RrAn7JKnbQG/uEtLMkBgFF2Fuihg="}
}
http = urllib4.PoolManager(
    spki_pins=pins,
    cert_transparency_policy=CertificateTransparencyPolicy.BEST_EFFORT
)

# Create an HSTS handler
hsts_cache = HSTSCache()
hsts_handler = HSTSHandler(hsts_cache)

# Secure a URL if needed
url = "http://example.com/api"
secured_url = hsts_handler.secure_url(url)  # Returns https://example.com/api if in HSTS cache

Contributing

This project is in its early stages and contributions are welcome! Here's how you can help:

  • Bug Reports: If you find a bug, please open an issue with detailed information.
  • Feature Requests: Have ideas for new features? Open an issue to discuss.
  • Code Contributions: Pull requests are welcome for bug fixes or new features.
  • Documentation: Help improve or expand the documentation.
  • Testing: Help write or improve tests for the codebase.

Development Guidelines

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

Relationship to urllib3

urllib4 is inspired by urllib3 but is not an official successor. While we aim for compatibility where possible, this project is experimental and may diverge from urllib3's API:

# urllib3 usage
import urllib3
http = urllib3.PoolManager()
response = http.request("GET", "https://example.com")

# Future urllib4 usage (when implemented)
import urllib4
http = urllib4.PoolManager()
response = http.request("GET", "https://example.com")

Security Considerations

urllib4 is designed with security in mind, providing robust SSL/TLS verification, certificate transparency checking, and HSTS support. It's suitable for use in production environments where security is a priority.

Acknowledgements

This project builds on concepts from the urllib3 project and other Python HTTP libraries. We extend our gratitude to the authors and maintainers of these projects for their foundational work.

License

This project is licensed under the MIT License - see the LICENSE file for details.

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

ai_urllib4-2.0.0.tar.gz (95.6 kB view details)

Uploaded Source

Built Distribution

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

ai_urllib4-2.0.0-py3-none-any.whl (110.3 kB view details)

Uploaded Python 3

File details

Details for the file ai_urllib4-2.0.0.tar.gz.

File metadata

  • Download URL: ai_urllib4-2.0.0.tar.gz
  • Upload date:
  • Size: 95.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for ai_urllib4-2.0.0.tar.gz
Algorithm Hash digest
SHA256 9e814ceb48c86c5db6e5aca80fb8ddee3205dd828f683be49a78b4e2824b472e
MD5 7517fc0a6c71ea12b267e76378649143
BLAKE2b-256 1444a86ba9fa7a50498bc70ec46160c6ca86aac94f8ee8478ca0dd16a6a0eb99

See more details on using hashes here.

File details

Details for the file ai_urllib4-2.0.0-py3-none-any.whl.

File metadata

  • Download URL: ai_urllib4-2.0.0-py3-none-any.whl
  • Upload date:
  • Size: 110.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for ai_urllib4-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 297b3ce7722aa30fd957caa8ded5952b98b9033700db03df8c8303c717d2fb0e
MD5 09bf4b6e3bd07d98688db41fddc915f3
BLAKE2b-256 66cdd8dea06fc86e7e48e8f97ab6e626e060f80ca5ee80082d6ab4f9185d934f

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