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