python httpx支持访问http2

  1. 安装包
pip install httpx[http2]
  1. demo
    requests 包无法访问http2的server,httpx 支持,只需要设施http2=True 即可
import httpx
import requests
res= requests.get('https://spa16.scrape.center/',verify=False)
print(res.status_code)

with httpx.Client(http2=True) as clients:
    response = clients.get('https://spa16.scrape.center/')
    print(response.text)

异步

import httpx
import asyncio

async def test(url):
    async with httpx.AsyncClient(http2=True) as client:
        response = await client.get(url)
        print(response.status_code)
if __name__=='__main__':
    asyncio.get_event_loop().run_until_complete(test('https://www.httpbin.org/get'))
上一篇:netty系列之:一个价值上亿的网站速度优化方案


下一篇:性能优化理解