| 1 |
Metadata-Version: 1.1 |
| 2 |
Name: http-parser |
| 3 |
Version: 0.9.0 |
| 4 |
Summary: http request/response parser |
| 5 |
Home-page: http://github.com/benoitc/http-parser |
| 6 |
Author: Benoit Chesneau |
| 7 |
Author-email: [email protected] |
| 8 |
License: MIT |
| 9 |
Description: http-parser |
| 10 |
----------- |
| 11 |
|
| 12 |
HTTP request/response parser for Python compatible with Python 2.x |
| 13 |
(>=2.7), Python 3 and Pypy. If possible a C parser based on |
| 14 |
http-parser_ from Ryan Dahl will be used. |
| 15 |
|
| 16 |
http-parser is under the MIT license. |
| 17 |
|
| 18 |
Project url: https://github.com/benoitc/http-parser/ |
| 19 |
|
| 20 |
.. image:: |
| 21 |
https://secure.travis-ci.org/benoitc/http-parser.png?branch=master |
| 22 |
:alt: Build Status |
| 23 |
:target: https://travis-ci.org/benoitc/http-parser |
| 24 |
|
| 25 |
Requirements: |
| 26 |
------------- |
| 27 |
|
| 28 |
- Python 2.7 or sup. Pypy latest version. |
| 29 |
- Cython if you need to rebuild the C code (Not needed for Pypy) |
| 30 |
|
| 31 |
Installation |
| 32 |
------------ |
| 33 |
|
| 34 |
:: |
| 35 |
|
| 36 |
$ pip install http-parser |
| 37 |
|
| 38 |
Or install from source:: |
| 39 |
|
| 40 |
$ git clone git://github.com/benoitc/http-parser.git |
| 41 |
$ cd http-parser && python setup.py install |
| 42 |
|
| 43 |
|
| 44 |
Note: if you get an error on MacOSX try to install with the following |
| 45 |
arguments: |
| 46 |
|
| 47 |
$ env ARCHFLAGS="-arch i386 -arch x86_64" python setup.py install |
| 48 |
|
| 49 |
Usage |
| 50 |
----- |
| 51 |
|
| 52 |
http-parser provide you **parser.HttpParser** low-level parser in C that |
| 53 |
you can access in your python program and **http.HttpStream** providing |
| 54 |
higher-level access to a readable,sequential io.RawIOBase object. |
| 55 |
|
| 56 |
To help you in your day work, http-parser provides you 3 kind of readers |
| 57 |
in the reader module: IterReader to read iterables, StringReader to |
| 58 |
reads strings and StringIO objects, SocketReader to read sockets or |
| 59 |
objects with the same api (recv_into needed). You can of course use any |
| 60 |
io.RawIOBase object. |
| 61 |
|
| 62 |
Example of HttpStream |
| 63 |
+++++++++++++++++++++ |
| 64 |
|
| 65 |
ex:: |
| 66 |
|
| 67 |
#!/usr/bin/env python |
| 68 |
import socket |
| 69 |
|
| 70 |
from http_parser.http import HttpStream |
| 71 |
from http_parser.reader import SocketReader |
| 72 |
|
| 73 |
def main(): |
| 74 |
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 75 |
try: |
| 76 |
s.connect(('gunicorn.org', 80)) |
| 77 |
s.send("GET / HTTP/1.1\r\nHost: gunicorn.org\r\n\r\n") |
| 78 |
r = SocketReader(s) |
| 79 |
p = HttpStream(r) |
| 80 |
print p.headers() |
| 81 |
print p.body_file().read() |
| 82 |
finally: |
| 83 |
s.close() |
| 84 |
|
| 85 |
if __name__ == "__main__": |
| 86 |
main() |
| 87 |
|
| 88 |
Example of HttpParser: |
| 89 |
++++++++++++++++++++++ |
| 90 |
|
| 91 |
:: |
| 92 |
|
| 93 |
#!/usr/bin/env python |
| 94 |
import socket |
| 95 |
|
| 96 |
# try to import C parser then fallback in pure python parser. |
| 97 |
try: |
| 98 |
from http_parser.parser import HttpParser |
| 99 |
except ImportError: |
| 100 |
from http_parser.pyparser import HttpParser |
| 101 |
|
| 102 |
|
| 103 |
def main(): |
| 104 |
|
| 105 |
p = HttpParser() |
| 106 |
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 107 |
body = [] |
| 108 |
try: |
| 109 |
s.connect(('gunicorn.org', 80)) |
| 110 |
s.send("GET / HTTP/1.1\r\nHost: gunicorn.org\r\n\r\n") |
| 111 |
|
| 112 |
while True: |
| 113 |
data = s.recv(1024) |
| 114 |
if not data: |
| 115 |
break |
| 116 |
|
| 117 |
recved = len(data) |
| 118 |
nparsed = p.execute(data, recved) |
| 119 |
assert nparsed == recved |
| 120 |
|
| 121 |
if p.is_headers_complete(): |
| 122 |
print p.get_headers() |
| 123 |
|
| 124 |
if p.is_partial_body(): |
| 125 |
body.append(p.recv_body()) |
| 126 |
|
| 127 |
if p.is_message_complete(): |
| 128 |
break |
| 129 |
|
| 130 |
print "".join(body) |
| 131 |
|
| 132 |
finally: |
| 133 |
s.close() |
| 134 |
|
| 135 |
if __name__ == "__main__": |
| 136 |
main() |
| 137 |
|
| 138 |
|
| 139 |
You can find more docs in the code (or use a doc generator). |
| 140 |
|
| 141 |
|
| 142 |
Copyright |
| 143 |
--------- |
| 144 |
|
| 145 |
2011-2020 (c) Benoît Chesneau <[email protected]> |
| 146 |
|
| 147 |
|
| 148 |
.. http-parser_ https://github.com/ry/http-parser |
| 149 |
|
| 150 |
Platform: any |
| 151 |
Classifier: Development Status :: 4 - Beta |
| 152 |
Classifier: Environment :: Other Environment |
| 153 |
Classifier: Intended Audience :: Developers |
| 154 |
Classifier: License :: OSI Approved :: MIT License |
| 155 |
Classifier: Operating System :: MacOS :: MacOS X |
| 156 |
Classifier: Operating System :: POSIX |
| 157 |
Classifier: Programming Language :: Python |
| 158 |
Classifier: Programming Language :: Python :: 2 |
| 159 |
Classifier: Programming Language :: Python :: 2.6 |
| 160 |
Classifier: Programming Language :: Python :: 2.7 |
| 161 |
Classifier: Programming Language :: Python :: 3 |
| 162 |
Classifier: Programming Language :: Python :: 3.2 |
| 163 |
Classifier: Programming Language :: Python :: 3.3 |
| 164 |
Classifier: Programming Language :: Python :: 3.4 |
| 165 |
Classifier: Programming Language :: Python :: 3.5 |
| 166 |
Classifier: Programming Language :: Python :: 3.6 |
| 167 |
Classifier: Programming Language :: Python :: 3.7 |
| 168 |
Classifier: Programming Language :: Python :: 3.8 |
| 169 |
Classifier: Topic :: Internet |
| 170 |
Classifier: Topic :: Utilities |
| 171 |
Classifier: Topic :: Software Development :: Libraries :: Python Modules |