Skip to main content

http-parser

HTTP request/response parser for Python compatible with Python 2.x (>=2.7), Python 3 and Pypy. If possible a C parser based on http-parser from Ryan Dahl will be used.

http-parser is under the MIT license.

Project url: https://github.com/benoitc/http-parser/

Build Status

Requirements:

  • Python 2.7 or sup. Pypy latest version.

  • Cython if you need to rebuild the C code (Not needed for Pypy)

Installation

$ pip install http-parser

Or install from source:

$ git clone git://github.com/benoitc/http-parser.git
$ cd http-parser && python setup.py install

Note: if you get an error on MacOSX try to install with the following arguments:

$ env ARCHFLAGS=”-arch i386 -arch x86_64” python setup.py install

Usage

http-parser provide you parser.HttpParser low-level parser in C that you can access in your python program and http.HttpStream providing higher-level access to a readable,sequential io.RawIOBase object.

To help you in your day work, http-parser provides you 3 kind of readers in the reader module: IterReader to read iterables, StringReader to reads strings and StringIO objects, SocketReader to read sockets or objects with the same api (recv_into needed). You can of course use any io.RawIOBase object.

Example of HttpStream

ex:

#!/usr/bin/env python
import socket

from http_parser.http import HttpStream
from http_parser.reader import SocketReader

def main():
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    try:
        s.connect(('gunicorn.org', 80))
        s.send("GET / HTTP/1.1\r\nHost: gunicorn.org\r\n\r\n")
        r = SocketReader(s)
        p = HttpStream(r)
        print p.headers()
        print p.body_file().read()
    finally:
        s.close()

if __name__ == "__main__":
    main()

Example of HttpParser:

#!/usr/bin/env python
import socket

# try to import C parser then fallback in pure python parser.
try:
    from http_parser.parser import HttpParser
except ImportError:
    from http_parser.pyparser import HttpParser


def main():

    p = HttpParser()
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    body = []
    try:
        s.connect(('gunicorn.org', 80))
        s.send("GET / HTTP/1.1\r\nHost: gunicorn.org\r\n\r\n")

        while True:
            data = s.recv(1024)
            if not data:
                break

            recved = len(data)
            nparsed = p.execute(data, recved)
            assert nparsed == recved

            if p.is_headers_complete():
                print p.get_headers()

            if p.is_partial_body():
                body.append(p.recv_body())

            if p.is_message_complete():
                break

        print "".join(body)

    finally:
        s.close()

if __name__ == "__main__":
    main()

You can find more docs in the code (or use a doc generator).

Metadata

Release files for http-parser 0.9.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for http-parser 0.9.0
File Size Uploaded
http-parser-0.9.0.tar.gz 167.1 kB Details

Built distributions (wheels)

Table of built distributions (wheels) for http-parser 0.9.0
File Interpreter ABI Platform
http_parser-0.9.0-py3.7-macosx-10.15-x86_64.egg Legacy Egg format - - Details
http_parser-0.9.0-cp37-cp37m-macosx_10_15_x86_64.whl CPython 3.7 CPython 3.7 pymalloc macOS 10.15+ x86-64 Details

Total release size: 324.1 kB

Release files / http-parser-0.9.0.tar.gz

Download URL http-parser-0.9.0.tar.gz
Size 167.1 kB
Tags Source
SHA-256 checksum
How to use checksums
441d3a57c6227230f98aa97539da006fda0f8f4880bc4c5cd895ff2138a52621
BLAKE2b-256 checksum
How to use checksums
04b2c98d0a2b759fcf91ae0a3e7691e840e4c861dac2c7f5a4e36220129d0cc8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/43.0.0 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.7.6

Release files / http_parser-0.9.0-py3.7-macosx-10.15-x86_64.egg

Download URL http_parser-0.9.0-py3.7-macosx-10.15-x86_64.egg
Size 86.1 kB
Tags Egg
SHA-256 checksum
How to use checksums
779f6666c32e831f755bf8c410df8a0d5a498b374506bfe0e74d5b3def746de3
BLAKE2b-256 checksum
How to use checksums
59b74ce92a69e3f77ffdcaf385780506f8078f6da92f04080c22cdfe9324245a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/43.0.0 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.7.6

Release files / http_parser-0.9.0-cp37-cp37m-macosx_10_15_x86_64.whl

Download URL http_parser-0.9.0-cp37-cp37m-macosx_10_15_x86_64.whl
Size 70.9 kB
Tags CPython 3.7 CPython 3.7 pymalloc macOS 10.15+ x86-64
SHA-256 checksum
How to use checksums
fc025894367cff34fcfc7386980b146b9498e9324cf3dc8992b39609e6158401
BLAKE2b-256 checksum
How to use checksums
686127b858a69bed58acd271717095d562178ef733d43c60cec6e3a7ca804028
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/43.0.0 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.7.6

Release history Release notifications | RSS feed

This release

0.9.0 This release

3 release files

0.8.3

1 release file

0.8.2

1 release file

0.8.1

1 release file

0.8.0

1 release file

0.7.12

1 release file

0.7.11

1 release file

0.7.10

1 release file

0.7.8

1 release file

0.7.7

1 release file

0.7.6

1 release file

0.7.5

1 release file

0.7.4

1 release file

0.7.3

1 release file

0.7.2

1 release file

0.7.1

1 release file

0.7.0

1 release file

0.6.3

1 release file

0.6.2

1 release file

0.6.1

1 release file

0.6.0

1 release file

0.5.4

1 release file

0.5.3

1 release file

0.5.2

1 release file

0.5.1

1 release file

0.5.0

1 release file

0.4.1

1 release file

0.3.3

1 release file

0.3.2

1 release file

0.3.1

1 release file

0.3.0

1 release file

0.2.0

1 release file

0.1.3

1 release file

0.1.2

1 release file

0.1.1

1 release file

0.1.0

1 release 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