超快的下一代 Python 异步 HTTP 库

586 阅读1分钟
原文链接: github.com

Super fast asynchronous HTTP client. For the future of python.

Build codecov License Pypi Python

Installation

pip install ashttp

Usage

Request

import ashttp as http

async def main():                                                                    
    r = await http.get('https://httpbin.org/get')                                                         
    print(r.status_code) # 200
    print(r.encoding) # 'ASCII' 
    print(await r.json())
    
if __name__ == '__main__':
    import asyncio
    loop = asyncio.get_running_loop()
    loop.run_until_complete(main())

Parse HTML

import ashttp as http

async def main():                                                                    
    r = await http.get('https://pypi.org/project/ashttp/')                                                    
    html = await r.html()
    assert html.find_one(".sponsors__name").text == "Elastic"
    assert html.find_one(".sponsors__sponsor").links
    assert html.find_one(".sponsors__sponsor").attrs
    assert html.find_one(".sponsors__sponsor").tag == "a"   

if __name__ == '__main__':
    import asyncio
    loop = asyncio.get_running_loop()
    loop.run_until_complete(main())

Todo

  • Keep-Alive & Connection Pooling
  • Basic/Digest Authentication
  • File Uploads