Giter Club home page Giter Club logo

pynghttp2's Introduction

pynghttp2

PyPi Version 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))

pynghttp2's People

Contributors

kahlertl avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

dutradda

pynghttp2's Issues

performance question

Hi,
first of all thanks for his lib. It makes really easy to use HTTP2 in python.
I hava a http2 framework using h2 (pure python) and now I compared it with this solution.
I used h2load to test an echo server (h2load -n10000 -c3 -m20 http://localhost:8080)
I expected that c binding is at least 10 time faster but it can handle 'only' ~3000msg/sec (the pure h2 soulution can handle 1120).
Using uvloop the result a bit better: 3680 vs 1288.

In h2load page I saw result like:
'finished in 7.08s, 141164.80 req/s, 555.33MB/s' :-O
(However it is possible that the latter was ran on multicore, while the python version is ran on single core. )

Do you have any measurement or advise how to make it even faster?

thanks

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.