一组独立于框架的http协议实用程序。
httptools的Python项目详细描述
httptools是nodejs http解析器的python绑定。它仍然在一个 很早的开发阶段,预计api会崩溃。
该包在pypi上可用:pip install httptools
。
API
httptools包含两个类httptools.HttpRequestParser
,
httptools.HttpResponseParser
和解析url的函数
httptools.parse_url
。有关示例,请参见单元测试。
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 """
开发
使用克隆此存储库
git clone --recursive git@github.com:MagicStack/httptools.git
使用Python3.5创建虚拟环境:
python3.5 -m venv envname
使用
激活环境source envname/bin/activate
用
pip install cython
安装cython运行
make
和make test
。
许可证
麻省理工学院。