带有回调请求的简单异步 python 3
simple async with callback request python 3
我是 python 的新手 我正在尝试开发发送大约 10,000 个请求来测试我的 API 的应用程序,我使用 pyqt5 和 python 3,我想要使用请求发出许多异步请求,发送许多请求而不是等待响应,而是在响应准备好时添加回调例如更新UI,请给我一个工作示例,谢谢帮助
您好 furas 试试您的代码,无需向 session.post 添加代理即可运行,这是代码
import aiohttp
import asyncio
import sys
import time
from decimal import localcontext, Decimal, ROUND_HALF_UP
from datetime import datetime,timezone ,timedelta
BASE_URI = 'https://d.com/session.php'
def show_exception_and_exit(exc_type, exc_value, tb):
traceback.print_exception(exc_type, exc_value, tb)
sys.exit(-1)
async def fetch(session, url):
proxies = {
"http" :"http://W09s1H:eMwmFL@91.188.240.67:9951",
"https" :"https://W09s1H:eMwmFL@91.188.240.67:9951"
}
Timestamp = int((datetime.utcnow() - datetime(1970,1,1)).total_seconds())
headers = {
"user-agent": "Mozilla/5.0 (X11; Linux i686; rv:64.0) Gecko/20100101 Firefox/64.0"
}
JsonSession = "{'device':'id'}"
async with session.post(url,
data=JsonSession,
headers=headers,
params={"timestamp":Timestamp}) as response:
return await response.text()
async def main(url):
tasks = []
results = []
async with aiohttp.ClientSession() as session:
for n in range(1,10):
tasks.append(fetch(session, url))
results = await asyncio.gather(*tasks)
return results
def parallel(url):
loop = asyncio.get_event_loop()
results = loop.run_until_complete(main(url))
return results
# --- main ---
result = parallel(BASE_URI)
for item in result:
print(item[:300])
print('-----')
当我像这样添加代理时:
async with session.post(url,
data=JsonSession,
headers=headers,
params={"timestamp":Timestamp},
proxies=proxies) as response:
我收到错误
Traceback (most recent call last):
File "C:/Users/soft/AppData/Local/Programs/Python/Python37-32/MyApp/Scripts/Test.py", line 51, in <module>
result = parallel(BASE_URI)
File "C:/Users/soft/AppData/Local/Programs/Python/Python37-32/MyApp/Scripts/Test.py", line 44, in parallel
results = loop.run_until_complete(main(url))
File "C:\Users\soft\AppData\Local\Programs\Python\Python37-32\lib\asyncio\base_events.py", line 579, in run_until_complete
return future.result()
File "C:/Users/soft/AppData/Local/Programs/Python/Python37-32/MyApp/Scripts/Test.py", line 39, in main
results = await asyncio.gather(*tasks)
File "C:/Users/soft/AppData/Local/Programs/Python/Python37-32/MyApp/Scripts/Test.py", line 30, in fetch
proxies=proxies) as response:
File "C:\Users\soft\AppData\Local\Programs\Python\Python37-32\lib\site-packages\aiohttp\client.py", line 876, in post
**kwargs))
TypeError: _request() got an unexpected keyword argument 'proxies'
如何将代理添加到 session.post 因为我确实需要添加它
我是 python 的新手 我正在尝试开发发送大约 10,000 个请求来测试我的 API 的应用程序,我使用 pyqt5 和 python 3,我想要使用请求发出许多异步请求,发送许多请求而不是等待响应,而是在响应准备好时添加回调例如更新UI,请给我一个工作示例,谢谢帮助
您好 furas 试试您的代码,无需向 session.post 添加代理即可运行,这是代码
import aiohttp
import asyncio
import sys
import time
from decimal import localcontext, Decimal, ROUND_HALF_UP
from datetime import datetime,timezone ,timedelta
BASE_URI = 'https://d.com/session.php'
def show_exception_and_exit(exc_type, exc_value, tb):
traceback.print_exception(exc_type, exc_value, tb)
sys.exit(-1)
async def fetch(session, url):
proxies = {
"http" :"http://W09s1H:eMwmFL@91.188.240.67:9951",
"https" :"https://W09s1H:eMwmFL@91.188.240.67:9951"
}
Timestamp = int((datetime.utcnow() - datetime(1970,1,1)).total_seconds())
headers = {
"user-agent": "Mozilla/5.0 (X11; Linux i686; rv:64.0) Gecko/20100101 Firefox/64.0"
}
JsonSession = "{'device':'id'}"
async with session.post(url,
data=JsonSession,
headers=headers,
params={"timestamp":Timestamp}) as response:
return await response.text()
async def main(url):
tasks = []
results = []
async with aiohttp.ClientSession() as session:
for n in range(1,10):
tasks.append(fetch(session, url))
results = await asyncio.gather(*tasks)
return results
def parallel(url):
loop = asyncio.get_event_loop()
results = loop.run_until_complete(main(url))
return results
# --- main ---
result = parallel(BASE_URI)
for item in result:
print(item[:300])
print('-----')
当我像这样添加代理时:
async with session.post(url,
data=JsonSession,
headers=headers,
params={"timestamp":Timestamp},
proxies=proxies) as response:
我收到错误
Traceback (most recent call last):
File "C:/Users/soft/AppData/Local/Programs/Python/Python37-32/MyApp/Scripts/Test.py", line 51, in <module>
result = parallel(BASE_URI)
File "C:/Users/soft/AppData/Local/Programs/Python/Python37-32/MyApp/Scripts/Test.py", line 44, in parallel
results = loop.run_until_complete(main(url))
File "C:\Users\soft\AppData\Local\Programs\Python\Python37-32\lib\asyncio\base_events.py", line 579, in run_until_complete
return future.result()
File "C:/Users/soft/AppData/Local/Programs/Python/Python37-32/MyApp/Scripts/Test.py", line 39, in main
results = await asyncio.gather(*tasks)
File "C:/Users/soft/AppData/Local/Programs/Python/Python37-32/MyApp/Scripts/Test.py", line 30, in fetch
proxies=proxies) as response:
File "C:\Users\soft\AppData\Local\Programs\Python\Python37-32\lib\site-packages\aiohttp\client.py", line 876, in post
**kwargs))
TypeError: _request() got an unexpected keyword argument 'proxies'
如何将代理添加到 session.post 因为我确实需要添加它