Skip to main content

HTTP client library for gevent

Project description

GitHub Workflow CI Status PyPI Python Version from PEP 621 TOML PyPI - Downloads

geventhttpclient

A high performance, concurrent HTTP client library for python using gevent.

gevent.httplib support was removed in gevent 1.0, geventhttpclient now provides that missing functionality.

geventhttpclient uses a fast http parser, written in C.

geventhttpclient has been specifically designed for high concurrency, streaming and support HTTP 1.1 persistent connections. More generally it is designed for efficiently pulling from REST APIs and streaming APIs like Twitter's.

Safe SSL support is provided by default. geventhttpclient depends on the certifi CA Bundle. This is the same CA Bundle which ships with the Requests codebase, and is derived from Mozilla Firefox's canonical set.

As of version 2.1, only Python 3.9+ is fully supported (with prebuilt wheels).

A simple example:

#!/usr/bin/python

from geventhttpclient import HTTPClient
from geventhttpclient.url import URL

url = URL('http://gevent.org/')

http = HTTPClient(url.host)

# issue a get request
response = http.get(url.request_uri)

# read status_code
response.status_code

# read response body
body = response.read()

# close connections
http.close()

httplib compatibility and monkey patch

geventhttpclient.httplib module contains classes for drop in replacement of httplib connection and response objects. If you use httplib directly you can replace the httplib imports by geventhttpclient.httplib.

# from httplib import HTTPConnection
from geventhttpclient.httplib import HTTPConnection

If you use httplib2, urllib or urllib2; you can patch httplib to use the wrappers from geventhttpclient. For httplib2, make sure you patch before you import or the super calls will fail.

import geventhttpclient.httplib
geventhttpclient.httplib.patch()

import httplib2

High Concurrency

HTTPClient has connection pool built in and is greenlet safe by design. You can use the same instance among several greenlets.

#!/usr/bin/env python

import gevent.pool
import json

from geventhttpclient import HTTPClient
from geventhttpclient.url import URL


# go to http://developers.facebook.com/tools/explorer and copy the access token
TOKEN = '<go to http://developers.facebook.com/tools/explorer and copy the access token>'

url = URL('https://graph.facebook.com/me/friends')
url['access_token'] = TOKEN

# setting the concurrency to 10 allow to create 10 connections and
# reuse them.
http = HTTPClient.from_url(url, concurrency=10)

response = http.get(url.request_uri)
assert response.status_code == 200

# response comply to the read protocol. It passes the stream to
# the json parser as it's being read.
data = json.load(response)['data']

def print_friend_username(http, friend_id):
    friend_url = URL('/' + str(friend_id))
    friend_url['access_token'] = TOKEN
    # the greenlet will block until a connection is available
    response = http.get(friend_url.request_uri)
    assert response.status_code == 200
    friend = json.load(response)
    if 'username' in friend:
        print('%s: %s' % (friend['username'], friend['name']))
    else:
        print('%s has no username.' % friend['name'])

# allow to run 20 greenlet at a time, this is more than concurrency
# of the http client but isn't a problem since the client has its own
# connection pool.
pool = gevent.pool.Pool(20)
for item in data:
    friend_id = item['id']
    pool.spawn(print_friend_username, http, friend_id)

pool.join()
http.close()

Streaming

geventhttpclient supports streaming. Response objects have a read(N) and readline() method that read the stream incrementally. See src/examples/twitter_streaming.py for pulling twitter stream API.

Here is an example on how to download a big file chunk by chunk to save memory:

#!/usr/bin/env python

from geventhttpclient import HTTPClient, URL

url = URL('http://127.0.0.1:80/100.dat')
http = HTTPClient.from_url(url)
response = http.get(url.query_string)
assert response.status_code == 200

CHUNK_SIZE = 1024 * 16 # 16KB
with open('/tmp/100.dat', 'w') as f:
    data = response.read(CHUNK_SIZE)
    while data:
        f.write(data)
        data = response.read(CHUNK_SIZE)

Benchmarks

The benchmark does 10000 get requests against a local nginx server in the default configuration with a concurrency of 10. See benchmarks folder. The requests per second for a couple of popular clients is given in the table below.

HTTP Client RPS
GeventHTTPClient 7181.2
Urllib3 2847.5
Httpx 1682.9
Requests 1186.4

Linux(x86_64), Python 3.11.6 @ Intel i7-7560U

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

geventhttpclient-2.1.1.tar.gz (79.7 kB view hashes)

Uploaded Source

Built Distributions

geventhttpclient-2.1.1-pp310-pypy310_pp73-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (67.6 kB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ x86-64 manylinux: glibc 2.5+ x86-64

geventhttpclient-2.1.1-pp310-pypy310_pp73-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (71.5 kB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

geventhttpclient-2.1.1-pp39-pypy39_pp73-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (67.6 kB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ x86-64 manylinux: glibc 2.5+ x86-64

geventhttpclient-2.1.1-pp39-pypy39_pp73-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (71.5 kB view hashes)

Uploaded PyPy manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

geventhttpclient-2.1.1-cp312-cp312-musllinux_1_1_x86_64.whl (130.7 kB view hashes)

Uploaded CPython 3.12 musllinux: musl 1.1+ x86-64

geventhttpclient-2.1.1-cp312-cp312-musllinux_1_1_ppc64le.whl (141.2 kB view hashes)

Uploaded CPython 3.12 musllinux: musl 1.1+ ppc64le

geventhttpclient-2.1.1-cp312-cp312-musllinux_1_1_i686.whl (131.7 kB view hashes)

Uploaded CPython 3.12 musllinux: musl 1.1+ i686

geventhttpclient-2.1.1-cp312-cp312-musllinux_1_1_aarch64.whl (135.0 kB view hashes)

Uploaded CPython 3.12 musllinux: musl 1.1+ ARM64

geventhttpclient-2.1.1-cp312-cp312-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (136.8 kB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ ppc64le

geventhttpclient-2.1.1-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (131.6 kB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ ARM64

geventhttpclient-2.1.1-cp312-cp312-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (126.8 kB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ x86-64 manylinux: glibc 2.5+ x86-64

geventhttpclient-2.1.1-cp312-cp312-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (128.0 kB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

geventhttpclient-2.1.1-cp311-cp311-musllinux_1_1_x86_64.whl (130.5 kB view hashes)

Uploaded CPython 3.11 musllinux: musl 1.1+ x86-64

geventhttpclient-2.1.1-cp311-cp311-musllinux_1_1_ppc64le.whl (141.4 kB view hashes)

Uploaded CPython 3.11 musllinux: musl 1.1+ ppc64le

geventhttpclient-2.1.1-cp311-cp311-musllinux_1_1_i686.whl (131.7 kB view hashes)

Uploaded CPython 3.11 musllinux: musl 1.1+ i686

geventhttpclient-2.1.1-cp311-cp311-musllinux_1_1_aarch64.whl (135.2 kB view hashes)

Uploaded CPython 3.11 musllinux: musl 1.1+ ARM64

geventhttpclient-2.1.1-cp311-cp311-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (136.6 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ ppc64le

geventhttpclient-2.1.1-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (131.1 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ ARM64

geventhttpclient-2.1.1-cp311-cp311-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (126.0 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64 manylinux: glibc 2.5+ x86-64

geventhttpclient-2.1.1-cp311-cp311-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (127.5 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

geventhttpclient-2.1.1-cp310-cp310-musllinux_1_1_x86_64.whl (129.6 kB view hashes)

Uploaded CPython 3.10 musllinux: musl 1.1+ x86-64

geventhttpclient-2.1.1-cp310-cp310-musllinux_1_1_ppc64le.whl (140.7 kB view hashes)

Uploaded CPython 3.10 musllinux: musl 1.1+ ppc64le

geventhttpclient-2.1.1-cp310-cp310-musllinux_1_1_i686.whl (130.8 kB view hashes)

Uploaded CPython 3.10 musllinux: musl 1.1+ i686

geventhttpclient-2.1.1-cp310-cp310-musllinux_1_1_aarch64.whl (134.4 kB view hashes)

Uploaded CPython 3.10 musllinux: musl 1.1+ ARM64

geventhttpclient-2.1.1-cp310-cp310-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (136.5 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ ppc64le

geventhttpclient-2.1.1-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (131.0 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ ARM64

geventhttpclient-2.1.1-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (126.0 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64 manylinux: glibc 2.5+ x86-64

geventhttpclient-2.1.1-cp310-cp310-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (127.4 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

geventhttpclient-2.1.1-cp39-cp39-musllinux_1_1_x86_64.whl (129.4 kB view hashes)

Uploaded CPython 3.9 musllinux: musl 1.1+ x86-64

geventhttpclient-2.1.1-cp39-cp39-musllinux_1_1_ppc64le.whl (140.4 kB view hashes)

Uploaded CPython 3.9 musllinux: musl 1.1+ ppc64le

geventhttpclient-2.1.1-cp39-cp39-musllinux_1_1_i686.whl (130.5 kB view hashes)

Uploaded CPython 3.9 musllinux: musl 1.1+ i686

geventhttpclient-2.1.1-cp39-cp39-musllinux_1_1_aarch64.whl (134.1 kB view hashes)

Uploaded CPython 3.9 musllinux: musl 1.1+ ARM64

geventhttpclient-2.1.1-cp39-cp39-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (136.3 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ ppc64le

geventhttpclient-2.1.1-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (130.7 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ ARM64

geventhttpclient-2.1.1-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (125.7 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64 manylinux: glibc 2.5+ x86-64

geventhttpclient-2.1.1-cp39-cp39-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (127.1 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page