A collection of framework independent HTTP protocol utils.
Project description
httptools is a Python binding for the nodejs HTTP parser.
The package is available on PyPI: pip install httptools
.
APIs
httptools contains two classes httptools.HttpRequestParser
,
httptools.HttpResponseParser
and a function for parsing URLs
httptools.parse_url
. See unittests for examples.
class HttpRequestParser:
def __init__(self, protocol):
"""HttpRequestParser
protocol -- a Python object with the following methods
(all optional):
- on_message_begin()
- on_url(url: bytes)
- on_header(name: bytes, value: bytes)
- on_headers_complete()
- on_body(body: bytes)
- on_message_complete()
- on_chunk_header()
- on_chunk_complete()
- on_status(status: bytes)
"""
def get_http_version(self) -> str:
"""Return an HTTP protocol version."""
def should_keep_alive(self) -> bool:
"""Return ``True`` if keep-alive mode is preferred."""
def should_upgrade(self) -> bool:
"""Return ``True`` if the parsed request is a valid Upgrade request.
The method exposes a flag set just before on_headers_complete.
Calling this method earlier will only yield `False`.
"""
def feed_data(self, data: bytes):
"""Feed data to the parser.
Will eventually trigger callbacks on the ``protocol``
object.
On HTTP upgrade, this method will raise an
``HttpParserUpgrade`` exception, with its sole argument
set to the offset of the non-HTTP data in ``data``.
"""
def get_method(self) -> bytes:
"""Return HTTP request method (GET, HEAD, etc)"""
class HttpResponseParser:
"""Has all methods except ``get_method()`` that
HttpRequestParser has."""
def get_status_code(self) -> int:
"""Return the status code of the HTTP response"""
def parse_url(url: bytes):
"""Parse URL strings into a structured Python object.
Returns an instance of ``httptools.URL`` class with the
following attributes:
- schema: bytes
- host: bytes
- port: int
- path: bytes
- query: bytes
- fragment: bytes
- userinfo: bytes
"""
Development
-
Clone this repository with
git clone --recursive git@github.com:MagicStack/httptools.git
-
Create a virtual environment with Python 3.5:
python3.5 -m venv envname
-
Activate the environment with
source envname/bin/activate
-
Install Cython with
pip install cython
-
Run
make
andmake test
.
License
MIT.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
httptools-0.1.0.tar.gz
(106.1 kB
view hashes)
Built Distributions
Close
Hashes for httptools-0.1.0-cp38-cp38-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b7ff7aa5c74c4732957c0722989a8c85c3102b01c4e5e37db64dc9923b485d70 |
|
MD5 | e03c3ecf0c3db017226d048a125859e8 |
|
BLAKE2b-256 | 8e78e4d6b1b233afa0230a67cae9975f132193cccb80e3d48fafdc13751a8b71 |
Close
Hashes for httptools-0.1.0-cp38-cp38-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 46c964b75908568f0da46aa17eb11bf2bd00f9571bc392e38d5eba34daabd627 |
|
MD5 | bff798631751dbdefb99c27e89e21c2e |
|
BLAKE2b-256 | 6cdc3c7c8a176e420870d9eba6001c9a156bd0c48e8cb7e21355645292020078 |
Close
Hashes for httptools-0.1.0-cp38-cp38-macosx_10_13_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d3a0f0c5ca38a6e5de11f114407dc143bdfcd13895fa35c57f0cca2b68606207 |
|
MD5 | da2bbfb917b83fa00e9914c4026a8e75 |
|
BLAKE2b-256 | 2542370e92e004111a0f87ba54864e293204e0fc6cfe6c0aa7025e06b5672520 |
Close
Hashes for httptools-0.1.0-cp37-cp37m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ee10c3f4e6c2b252c9a5f2d550fb8aaf364551444527049856ac8c195092e75c |
|
MD5 | ff917074cf3e4c50387b990f15acc701 |
|
BLAKE2b-256 | f353748568550dc405eafda19736a297918cbf2438454e9cc0c53fb2c614fe72 |
Close
Hashes for httptools-0.1.0-cp37-cp37m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 86e777dbca5b82b23aa6939b98c80466bb9214790d927e870c26dffe8bf14ad0 |
|
MD5 | f85a254897015f6fb08c26bd2ee42644 |
|
BLAKE2b-256 | dd6ce24e4eb10475d33d34c213280e7016af402ea2b5ab08edeba65e03e52c47 |
Close
Hashes for httptools-0.1.0-cp37-cp37m-macosx_10_13_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 31f6a76aa797a64447a36abc4b92fdeb6afe1726fe1b2516d50eedbcf98b1739 |
|
MD5 | bef5b52d560344fc131c3e13f24ffc26 |
|
BLAKE2b-256 | ce5e3e4c9ddc6e5f32d4b3aa4c951a60a9b168408ff826cfa0b96f1385f03d23 |
Close
Hashes for httptools-0.1.0-cp36-cp36m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b89a46936bac45b2352818161819b10ed56cb8bb756addca162a3baf7606da64 |
|
MD5 | ffe61eaa3411abe8f73e936e59ca85c9 |
|
BLAKE2b-256 | 2c6932294704948815eab6ebd568f4dea2776ef7f2571502a66ccbf55c188af6 |
Close
Hashes for httptools-0.1.0-cp36-cp36m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c6d5eadcdc1c6541bdf10a7dd4fa8949a1e46e99fa292b44159f0990ce7977ce |
|
MD5 | 41c428971e9aae9ca0273661c4ee12c0 |
|
BLAKE2b-256 | a86983458510342365585969322a47d065b3adcccbcd34246bdcceb73e153e14 |
Close
Hashes for httptools-0.1.0-cp36-cp36m-macosx_10_13_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8bf3bbf76c97da8a4983051577d65db7eb86f2d1a38dca5cdcba5d2efb2a39f4 |
|
MD5 | 1cf878fe99b16cf9a7590ff1f2c5cae9 |
|
BLAKE2b-256 | 8c400e31b95fb0acfe4ec65dcde120e126a1f757c09ed92735def0f3ad11b890 |