Quic client/server protocol wrapper for Python.
Project description
PyQuic - Simple QUIC Client/Server Library
PyQuic is a Python library that provides a simplified, fluent API for building QUIC clients and servers using the aioquic library. It abstracts away the complexity of QUIC protocol handling while providing a clean, builder-pattern interface for both client and server implementations.
Features
- Fluent Builder API: Chain configuration methods for clean, readable code
- Async/Threaded Architecture: Runs QUIC operations in background threads with asyncio
- Per-Request Streams: Each client request uses a fresh bidirectional stream
- Custom Handler Support: Pluggable server-side business logic
- TLS Support: Built-in certificate handling with optional insecure mode for development
- Concurrent Requests: Multiple simultaneous requests supported on the client side
Architecture
Server (PyQuicServer)
The server uses a handler-based architecture where you provide a function to process incoming data:
def handler(data: bytes) -> bytes:
# Your business logic here
return processed_data
The server runs in its own daemon thread and uses asyncio internally to handle QUIC events and execute handlers in a thread pool.
Client (PyQuicClient)
The client maintains a persistent QUIC connection and allows you to send multiple concurrent requests. Each request:
- Opens a new bidirectional stream
- Sends data and closes the stream
- Returns a
concurrent.futures.Futurethat resolves when the server responds
Quick Start
Basic Echo Server and Client
import time
from py_quic import PyQuicClient, PyQuicServer
def echo_handler(data: bytes) -> bytes:
return data # Simple echo
# Start server
server = (PyQuicServer()
.with_host("127.0.0.1")
.with_port(4433)
.with_cert("cert.pem")
.with_key("key.pem")
.with_handler(echo_handler)
.start())
time.sleep(0.5) # Let server bind
# Start client
client = (PyQuicClient()
.with_host("127.0.0.1")
.with_port(4433)
.insecure() # Skip cert verification for self-signed certs
.start())
# Send concurrent requests
fut1 = client.send_message("Hello over QUIC!")
fut2 = client.send_message("How you doing?")
print(fut1.result()) # "Hello over QUIC!"
print(fut2.result()) # "How you doing?"
client.close()
API Reference
PyQuicServer
Configuration Methods (Fluent)
.with_host(host: str)- Set server bind address (default: "127.0.0.1").with_port(port: int)- Set server port (default: 4433).with_cert(cert: str)- Set TLS certificate file path (default: "cert.pem").with_key(key: str)- Set TLS private key file path (default: "key.pem").with_handler(fn: Callable[[bytes], bytes])- Set request handler function
Lifecycle Methods
.start()- Start server in background thread (non-blocking).start_and_wait()- Start server and block current thread
Handler Function Signature
def handler(data: bytes) -> bytes | bytearray | str | None:
# Process incoming data
# Return None to send no response
# Return str/bytes/bytearray to send back to client
pass
PyQuicClient
Configuration Methods (Fluent)
.with_host(host: str)- Set server address (default: "127.0.0.1").with_port(port: int)- Set server port (default: 4433).with_server_name(server_name: str)- Set SNI for TLS validation.insecure(value: bool = True)- Skip certificate verification (dev only)
Lifecycle Methods
.start()- Connect to server and start background thread.close()- Close connection and cleanup resources
Request Methods
.send_message(message: str, *, timeout: float | None = None)- Send message and returnFuture[str]
Advanced Usage
Custom Processing Handler
import json
def json_processor(data: bytes) -> bytes:
try:
# Parse incoming JSON
request = json.loads(data.decode())
# Process request
response = {
"echo": request,
"timestamp": time.time(),
"processed": True
}
# Return JSON response
return json.dumps(response).encode()
except Exception as e:
return json.dumps({"error": str(e)}).encode()
server = (PyQuicServer()
.with_handler(json_processor)
.start())
Multiple Concurrent Requests
client = PyQuicClient().with_host("example.com").start()
# Send multiple requests concurrently
futures = []
for i in range(10):
fut = client.send_message(f"Request {i}")
futures.append(fut)
# Collect all results
results = [fut.result() for fut in futures]
print(results)
client.close()
With Timeout
try:
future = client.send_message("Hello", timeout=5.0)
response = future.result()
print(f"Response: {response}")
except asyncio.TimeoutError:
print("Request timed out")
Requirements
- Python 3.10+
aioquiclibrary- TLS certificates (for production) or use
.insecure()for development
TLS Setup
For development, you can generate self-signed certificates:
openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -days 365 -nodes
Then use .insecure() on the client to skip certificate verification.
Performance
PyQuic achieves high-throughput concurrent request processing by leveraging QUIC's multiplexed streams over a single persistent connection.
Benchmark Results
- 1,000 concurrent requests completed in 0.95 seconds (~1,052 req/sec)
- Single connection with multiplexed bidirectional streams
- Zero head-of-line blocking between concurrent requests
- Memory efficient with per-stream buffers and automatic cleanup
Key Performance Features
- Stream Multiplexing: Each request uses a dedicated bidirectional stream
- Connection Reuse: Single QUIC connection handles all concurrent requests
- Concurrent Processing: Server executes handlers in thread pool
- Low Latency: Benefits from QUIC's 0-RTT/1-RTT connection establishment
- Flow Control: Automatic QUIC-level congestion and flow management
Protocol Details
- ALPN: Uses "echo" as the ALPN protocol identifier
- Streams: Each client request uses a new bidirectional stream
- Flow: Client sends data + end_stream, server responds + mirrors end_stream
- Threading: Server and client run in separate daemon threads with their own asyncio event loops
- Concurrency: Multiple streams can be active simultaneously per connection
Error Handling
- Server handler exceptions are caught and logged (but don't crash the server)
- Client connection failures raise exceptions during
.start() - Request timeouts raise
asyncio.TimeoutError - Network errors propagate through the
Future.result()call
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file py_quic-0.1.5.tar.gz.
File metadata
- Download URL: py_quic-0.1.5.tar.gz
- Upload date:
- Size: 11.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1ec2846884222543c1182658d8b310f03c942b6f95446762ede290f5718e16e2
|
|
| MD5 |
85e87ccf3ba764e2e7d8fd976b97539b
|
|
| BLAKE2b-256 |
8724282cfeaccc9ab07e896910a1e9673467c8a8b3dd85bfee8fcde314865f40
|
File details
Details for the file py_quic-0.1.5-py3-none-any.whl.
File metadata
- Download URL: py_quic-0.1.5-py3-none-any.whl
- Upload date:
- Size: 8.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b855b22a3bd14ae9a230be0a1b00695c4a7c50568c95d7233b465519495e3ec9
|
|
| MD5 |
913c2199603069feff19e8d4043107c8
|
|
| BLAKE2b-256 |
88da50115eacc3db5518923ada19ec74eb644cb6671bd05657a781c026a2924a
|