3 Star 2 Fork 1

kuaidaili/python-sdk

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
py3_aiohttp.py 1.16 KB
一键复制 编辑 原始数据 按行查看 历史
xuanyuwu 提交于 3年前 . token update
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
使用aiohttp请求代理服务器
请求http和https网页均适用
"""
import random
import asyncio
import aiohttp
import requests
page_url = "http://icanhazip.com/" # 要访问的目标网页
# API接口,返回格式为json
api_url = "http://dps.kdlapi.com/api/getdps/?secret_id=o1fjh1re9o28876h7c08&signature=xxxxx&num=5&pt=1&format=json&sep=1" # API接口
# API接口返回的proxy_list
proxy_list = requests.get(api_url).json().get('data').get('proxy_list')
# 用户名密码认证(私密代理/独享代理)
username = "username"
password = "password"
proxy_auth = aiohttp.BasicAuth(username, password)
async def fetch(url):
async with aiohttp.ClientSession() as session:
async with session.get(url, proxy="http://" + random.choice(proxy_list), proxy_auth=proxy_auth) as resp:
content = await resp.read()
print(f"status_code: {resp.status}, content: {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()
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/kuaidaili/python-sdk.git
git@gitee.com:kuaidaili/python-sdk.git
kuaidaili
python-sdk
python-sdk
master

搜索帮助