Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Fast HTTP parser

License

NotificationsYou must be signed in to change notification settings

MagicStack/httptools

Repository files navigation

Tests

httptools is a Python binding for the nodejs HTTP parser.

The package is available on PyPI:pip install httptools.

APIs

httptools contains two classeshttptools.HttpRequestParser,httptools.HttpResponseParser (fulfilled throughllhttp) and a function forparsing URLshttptools.parse_url (throughhttp-parse for now).See unittests for examples.

classHttpRequestParser: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)        """defget_http_version(self)->str:"""Return an HTTP protocol version."""defshould_keep_alive(self)->bool:"""Return ``True`` if keep-alive mode is preferred."""defshould_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`."""deffeed_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``.        """defget_method(self)->bytes:"""Return HTTP request method (GET, HEAD, etc)"""classHttpResponseParser:"""Has all methods except ``get_method()`` that    HttpRequestParser has."""defget_status_code(self)->int:"""Return the status code of the HTTP response"""defparse_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 withgit clone --recursive git@github.com:MagicStack/httptools.git

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

  3. Activate the environment withsource envname/bin/activate

  4. Runmake andmake test.

License

MIT.


[8]ページ先頭

©2009-2025 Movatter.jp