A collection of framework independent HTTP protocol utils.
Project Links
Meta
Author: Yury Selivanov
Requires Python: >=3.9
Classifiers
Intended Audience
- Developers
Programming Language
- Python :: 3
Operating System
- POSIX
- MacOS :: MacOS X
Environment
- Web Environment
Development Status
- 5 - Production/Stable
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
(fulfilled through
llhttp) and a function for
parsing URLs httptools.parse_url
(through
http-parse for now).
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:
python3 -m venv envname
-
Activate the environment with
source envname/bin/activate
-
Run
make
andmake test
.
License
MIT.
Oct 10, 2025
0.7.1
Oct 16, 2024
0.6.4
Oct 16, 2024
0.6.3
Oct 14, 2024
0.6.2
Oct 16, 2023
0.6.1
Jul 06, 2023
0.6.0
Sep 13, 2022
0.5.0
Feb 22, 2022
0.4.0
Aug 10, 2021
0.3.0
Apr 23, 2021
0.2.0
Apr 26, 2021
0.1.2
Feb 08, 2020
0.1.1
Feb 05, 2020
0.1.0
Feb 25, 2019
0.0.13
Feb 25, 2019
0.0.12
Mar 29, 2018
0.0.11
Dec 15, 2017
0.0.10
May 08, 2016
0.0.9
May 07, 2016
0.0.8
May 07, 2016
0.0.7
May 07, 2016
0.0.6
Apr 27, 2016
0.0.5
Apr 25, 2016
0.0.4
Oct 30, 2015
0.0.3
Oct 30, 2015
0.0.2
Oct 29, 2015
0.0.1
Wheel compatibility matrix
Files in release
No dependencies