使用aiohttp的一个小例子
#!/usr/bin/env python # encoding: utf-8 import aiohttp import asyncio import traceback import time import random TEST_URL="http://www.baidu.com" CODE=[200] class TestAio(object): async def getbaidu(self): conn=aiohttp.TCPConnector(verify_ssl=False) #忽略证书错误 async with aiohttp.ClientSession(connector=conn) as session: try: async with session.get(TEST_URL,timeout=15) as response: if response.status in CODE: print(response.text) except: print(traceback.format_exc()) def run(self): try: loop=asyncio.get_event_loop() for i in range(0,100,10): tasks=[self.getbaidu() for i in range(0,10)] loop.run_until_complete(asyncio.wait(tasks)) time.sleep(random.randint(1,5)) except: print(traceback.format_exc())