- import random
- import string
- import aiomysql
- import aiohttp
- import asyncio
- import time
- sem = asyncio.Semaphore(500)
- async def httpget_async(ccc):
- async with sem:
- try:
- async with aiohttp.ClientSession(conn_timeout=5,headers = {'User-Agent': ('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) ')}) as session:
- url='https://blog.csdn.net/weixin_41955327/article/details/84350847'
- async with session.get(f'{url}') as resp:
- # print(await resp.text())
- mmmm=await resp.read()
- return 1
- except Exception as e:
- return 0
- def bingfa_ceshi():
- tasks=[]
- for i in range(255):
- print('task::',i)
- task=asyncio.ensure_future(httpget_async(i))
- tasks.append(task)
- result=loop.run_until_complete(asyncio.gather(*tasks))
- kkk=sum(result)
- print(kkk)
- if __name__=='__main__':
- loop = asyncio.get_event_loop()
- a=time.clock()
- print('已经开始!')
- bingfa_ceshi()
- print('end:',time.clock()-a)
复制代码 |