Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/benoitc/http-parser
HTTP request/response parser for python in C
https://github.com/benoitc/http-parser
Last synced: about 2 months ago
JSON representation
HTTP request/response parser for python in C
- Host: GitHub
- URL: https://github.com/benoitc/http-parser
- Owner: benoitc
- License: other
- Created: 2011-04-14T15:33:36.000Z (over 13 years ago)
- Default Branch: master
- Last Pushed: 2024-05-21T14:05:11.000Z (4 months ago)
- Last Synced: 2024-07-19T00:39:33.373Z (2 months ago)
- Language: C
- Homepage:
- Size: 3.8 MB
- Stars: 342
- Watchers: 16
- Forks: 98
- Open Issues: 21
-
Metadata Files:
- Readme: README.rst
- License: LICENSE
Awesome Lists containing this project
README
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/
.. image::
https://secure.travis-ci.org/benoitc/http-parser.png?branch=master
:alt: Build Status
:target: https://travis-ci.org/benoitc/http-parserRequirements:
-------------- 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 installNote: 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 socketfrom http_parser.http import HttpStream
from http_parser.reader import SocketReaderdef 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 HttpParserdef 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:
breakrecved = len(data)
nparsed = p.execute(data, recved)
assert nparsed == recvedif p.is_headers_complete():
print p.get_headers()if p.is_partial_body():
body.append(p.recv_body())if p.is_message_complete():
breakprint "".join(body)
finally:
s.close()if __name__ == "__main__":
main()You can find more docs in the code (or use a doc generator).
Copyright
---------2011-2020 (c) Benoît Chesneau
.. http-parser_ https://github.com/ry/http-parser