Skip to main content
PyPi Version Build Status Code Coverage Documentation Status

pynghttp2 are simple asyncio Python bindings based on ctypes for the nghttp2 library. The only thing you need is a libnghttp2 version on your system.

On Debian-based systems you can install nghttp2 simply via apt:

apt-get install libnghttp2-14

The project was created in the context of a student work for an HTTP/2 protocol gateway in the µPCN project - an implementation of Delay-tolerant Networking (DTN) protocols.

Installation

pip install pynghttp2

Examples

High-Level API

from pynghttp2 import http2

# GET request
resp = await http2.get('http://localhost:64602/ping')

content = await resp.text()
assert content == 'pong'

# POST request
message = b"Lorem ipsum dolorem"
resp = await http2.post('http://localhost:64602/echo', data=message)
echo = await resp.read()
assert echo == message

Client Session

from pynghttp2 import ClientSession

# Multiplex two requests
async with ClientSession(host='localhost', port=64602) as session:
    stream1 = session.get('http://localhost:64602/stream')
    stream2 = session.get('http://localhost:64602/stream')

    await asyncio.gather(stream1.read(), stream2.read())

Server Session

import asyncio
from pynghttp2 import ServerSession

async def handle_request(req):
    """Echo the request body"""
    msg = await req.read()
    await req.response(200, data=msg)

with ServerSession(host='localhost', port=8080) as session:
    while True:
        # Wait for next incoming request
        req = await session

        # Handle each request in its own task to be able to multiplex
        # multiple requests and responses
        asyncio.ensure_future(handle_request(req))

Download files

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

Source Distribution

pynghttp2-0.2.2.tar.gz (20.8 kB view details)

Uploaded Source

File details

Details for the file pynghttp2-0.2.2.tar.gz.

File metadata

  • Download URL: pynghttp2-0.2.2.tar.gz
  • Upload date:
  • Size: 20.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for pynghttp2-0.2.2.tar.gz
Algorithm Hash digest
SHA256 3b8ba914715c1b49a72bd39b0967cd127d765c5077a67b4237304b3d0436fde1
MD5 adec0e8959311ee67cadccb38ebd0e11
BLAKE2b-256 15f52bb062e509a91775e70dc1188fe13a870b7deffc27b034e83fcc964f4170

See more details on using hashes here.

Release history Release notifications | RSS feed

0.3.2

2 files

0.3.1

1 file

This release

0.2.2 This release

1 file

0.1.5

1 file

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