httptools

A collection of framework independent HTTP protocol utils.

27 个版本 Python >=3.9
安装
pip install httptools
poetry add httptools
pipenv install httptools
conda install httptools
描述

Tests

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

  1. Clone this repository with git clone --recursive git@github.com:MagicStack/httptools.git

  2. Create a virtual environment with Python 3: python3 -m venv envname

  3. Activate the environment with source envname/bin/activate

  4. Run make and make test.

License

MIT.

版本列表
0.8.0 2026-05-25
0.7.1 2025-10-10
0.6.4 2024-10-16
0.6.3 2024-10-16
0.6.2 2024-10-14
0.6.1 2023-10-16
0.6.0 2023-07-06
0.5.0 2022-09-13
0.4.0 2022-02-22
0.3.0 2021-08-10
0.2.0 2021-04-23
0.1.2 2021-04-26
0.1.1 2020-02-08
0.1.0 2020-02-05
0.0.13 2019-02-25
0.0.12 2019-02-25
0.0.11 2018-03-29
0.0.10 2017-12-15
0.0.9 2016-05-08
0.0.8 2016-05-07
0.0.7 2016-05-07
0.0.6 2016-05-07
0.0.5 2016-04-27
0.0.4 2016-04-25
0.0.3 2015-10-30
0.0.2 2015-10-30
0.0.1 2015-10-29