我有一个为我编写的脚本,我无法执行它...我收到以下错误...
回溯(最近的呼叫最后):
文件“crawler.py”,第56行,在loop.run_until_complete(future)文件“C:\ Users \ lisa \ AppData \ Local \ Programs \ Python \ Python37-32 \ lib \ asyncio \ base_events.py”,第568行,在run_until_complete中返回future.result()文件“crawler.py”,第51行,在运行中等待响应文件“crawler.py”,第32行,在bound_fetch中等待获取(url,session)文件“crawler.py”,行22,使用session.get(url,headers = headers)作为响应获取异步:文件“C:\ Users \ lisa \ AppData \ Local \ Programs \ Python \ Python37-32 \ lib \ site-packages \ aiohttp \ client . py“,第843行,在aenter self._resp = await self._coro文件”C:\ Users \ lisa \ AppData \ Local \ Programs \ Python \ Python37-32 \ lib \ site-packages \ aiohttp \ client.py“,第387行,在_request中等待resp.start(conn)文件“C:\ Users \ lisa \ AppData \ Local \ Programs \ Python \ Python37-32 \ lib \ site-packages \ aiohttp \ client_reqrep.py”,第748行,启动消息,payload = await self._protocol.read()文件“C:\ Users \ lisa \ AppData \ Local \ Programs \ Python \ Python37-32 \ lib \ site-packages \ aiohttp \ streams.py”,line 533,在读取等待self._waiter aiohttp.client_exceptions.ServerDisconnectedError:无
有什么明显的东西我不见了吗?我可以在没有线程的情况下运行相同的脚本,谢谢......
import random
import asyncio
from aiohttp import ClientSession
import requests
from itertools import product
from string import *
from multiprocessing import Pool
from itertools import islice
import sys
headers = {'User-Agent': 'Mozilla/5.0'}
letter = sys.argv[1]
number = int(sys.argv[2])
first_group = product(ascii_lowercase, repeat=2)
second_group = product(digits, repeat=3)
codeList = [''.join([''.join(k) for k in prod]) for prod in product([letter], first_group, second_group)]
async def fetch(url, session):
async with session.get(url, headers=headers) as response:
statusCode = response.status
if(statusCode == 200):
print("{} statusCode is {}".format(url, statusCode))
return await response.read()
async def bound_fetch(sem, url, session):
async with sem:
await fetch(url, session)
def getUrl(codeIdex):
return "https://www.blahblah.com/" + codeList[codeIdex] + ".png"
async def run(r):
tasks = []
sem = asyncio.Semaphore(1000)
async with ClientSession() as session:
for i in range(r):
task = asyncio.ensure_future(bound_fetch(sem, getUrl(i), session))
tasks.append(task)
responses = asyncio.gather(*tasks)
await responses
loop = asyncio.get_event_loop()
future = asyncio.ensure_future(run(number))
loop.run_until_complete(future)