Python3使用httpx调用http代理IP的代码样例
站大爷
官方
2024-01-19
2383 浏览
温馨提示:
1. httpx的代码样例支持访问http,https网页
2. httpx不是python原生库,需要安装才能使用: pip install httpx
3. httpx暂时还不支持SOCKS代理
import random
import asyncio
import httpx
import requests
page_url ="http://example.com" #要访问的目标网页
#用户名密码授权
username="username"
password="password"
async def fetch(url):
proxies={
"http":"http://username:password@168.168.168.168:12345,
"https":http://username:password@168.168.168.168:12345
}
async with httpx.AsyncClient(proxies=proxies,timeout=10) as client:
resp=await client.get(url)
print(f"status_code:{resp.status_code},content:{resp.content}")
def run():
loop=asyncio.get_event_loop()
#异步发出5次请求
tasks=[fetch(page_url) for_in range(5)]
loop.run_until_complete(asyncio.wait(tasks))
if__name__=='__main__':
run()