Python教程

python httpx支持访问http2

本文主要是介绍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'))
这篇关于python httpx支持访问http2的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!