Skip to main content

http request/response parser

Project description

http-parser

HTTP request/response parser for Python compatible with Python 2.x (>=2.6), 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.6 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).

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

toil-http-parser-0.8.4.tar.gz (99.1 kB view details)

Uploaded Source

Built Distribution

toil_http_parser-0.8.4-py3.7-linux-x86_64.egg (228.4 kB view details)

Uploaded Source

File details

Details for the file toil-http-parser-0.8.4.tar.gz.

File metadata

  • Download URL: toil-http-parser-0.8.4.tar.gz
  • Upload date:
  • Size: 99.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: Python-urllib/3.7

File hashes

Hashes for toil-http-parser-0.8.4.tar.gz
Algorithm Hash digest
SHA256 3bf3d2a446a13df3ebc6a656b406754bb7af19483338a1e64fc27353a581d465
MD5 218701c42f0d8ad2183704b7a1f9b6d1
BLAKE2b-256 a96b6e007d2f56856f0cf1e79dfacbbfd09799f9fd45d1af8095fcbc81fc593d

See more details on using hashes here.

File details

Details for the file toil_http_parser-0.8.4-py3.7-linux-x86_64.egg.

File metadata

File hashes

Hashes for toil_http_parser-0.8.4-py3.7-linux-x86_64.egg
Algorithm Hash digest
SHA256 81472ca821e51fac0c0b9763b555281b109fd658647fa0811b5c1e2e7b485260
MD5 2e3bd12b605ba22a9313d91c63380068
BLAKE2b-256 c2b9d2fce35320b603e2141cf6a2ab0914c880adcfbb0b2c14cdd84af3a5e871

See more details on using hashes here.

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