-
Notifications
You must be signed in to change notification settings - Fork 0
/
ip_test.py
38 lines (26 loc) · 847 Bytes
/
ip_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import aiohttp
import asyncio
from datetime import datetime
from fake_useragent import UserAgent
async def fetch(client):
ua = UserAgent()
async with client.get('https://www.checkip.org/', proxy='http://127.0.0.1:7890', headers={
'User-Agent': ua.random,
'Host': 'www.checkip.org', }) as resp:
assert resp.status == 200
return await resp.text()
async def main():
async with aiohttp.ClientSession() as client:
html = await fetch(client)
print(html)
if __name__ == '__main__':
loop = asyncio.get_event_loop()
tasks = []
for i in range(2):
task = loop.create_task(main())
tasks.append(task)
start = datetime.now()
loop.run_until_complete(main())
end = datetime.now()
print("aiohttp版爬虫花费时间为:")
print(end - start)