gpt4 book ai didi

Python - ConnectionRefusedError、urllib3.exceptions.NewConnectionError 和 Colorama

转载 作者:行者123 更新时间:2023-12-01 08:42:25 24 4
gpt4 key购买 nike

所以今天我使用了自己编写的脚本,但我发现有些问题。我使用多重处理运行了我的程序几个小时,然后遇到了不同的错误。

第一个是:

Problem1

第二个是:

Problem2

最后一张:

Traceback (most recent call last):
self.wrapped.write(text[start:end])
File "C:\Program Files\Python36\lib\site-packages\colorama\ansitowin32.py", line 40, in write
self.__convertor.write(text)
File "C:\Program Files\Python36\lib\site-packages\colorama\ansitowin32.py", line 141, in write
self.write_and_convert(text)
File "C:\Program Files\Python36\lib\site-packages\colorama\ansitowin32.py", line 169, in write_and_convert
File "C:\Program Files\Python36\lib\site-packages\colorama\ansitowin32.py", line 40, in write
self.__convertor.write(text)
File "C:\Program Files\Python36\lib\site-packages\colorama\ansitowin32.py", line 141, in write
self.write_and_convert(text)
File "C:\Program Files\Python36\lib\site-packages\colorama\ansitowin32.py", line 169, in write_and_convert
self.write_plain_text(text, cursor, len(text)) File "C:\Program Files\Python36\lib\site-packages\colorama\ansitowin32.py", line 174, in write_plain_text
self.wrapped.write(text[start:end])
File "C:\Program Files\Python36\lib\site-packages\colorama\ansitowin32.py", line 40, in write
self.__convertor.write(text)
File "C:\Program Files\Python36\lib\site-packages\colorama\ansitowin32.py", line 141, in write
self.write_and_convert(text)
self.wrapped.write(text[start:end])
File "C:\Program Files\Python36\lib\site-packages\colorama\ansitowin32.py", line 40, in write
self.__convertor.write(text)
File "C:\Program Files\Python36\lib\site-packages\colorama\ansitowin32.py", line 141, in write
self.write_and_convert(text)
File "C:\Program Files\Python36\lib\site-packages\colorama\ansitowin32.py", line 169, in write_and_convert
File "C:\Program Files\Python36\lib\site-packages\colorama\ansitowin32.py", line 169, in write_and_convert
self.write_plain_text(text, cursor, len(text))
File "C:\Program Files\Python36\lib\site-packages\colorama\ansitowin32.py", line 174, in write_plain_text
self.wrapped.write(text[start:end])
File "C:\Program Files\Python36\lib\site-packages\colorama\ansitowin32.py", line 40, in write
self.__convertor.write(text)

问题是我试图搜索所有这三个问题,但我没有真正找到Python的信息,但我想问你们错误是什么意思,我可以了解它们的作用,而且我也想知道如何如果尝试命中,我将能够避免它们/或重试 - 除非我假设?

我所做的类似事情如下:

logger = Logger(value)
while True:

try:
url = 'https://www.google.com'

headers = {
'User-Agent': ('Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36'
' (KHTML, like Gecko) Chrome/68.0.3440.75 Safari/537.36')
}

requests.packages.urllib3.disable_warnings()
resp = requests.get(url, headers=headers, verify=False, proxies=get_random_proxy(), timeout=12)
resp.raise_for_status()

if resp.status_code == 200 or 301:
return resp.url

except HTTPError as err:
randomtime = random.randint(0, 1)
logger.error('Error HTTPError! - Retrying in {} secs'.format(randomtime))
time.sleep(randomtime)
continue


except requests.exceptions.ConnectionError as err:
randomtime = random.randint(0, 1)
logger.error('Error ConnectionError proxy! - Retrying in {} secs'.format(randomtime))
time.sleep(randomtime)
continue

except requests.exceptions.RequestException as err:
randomtime = random.randint(0, 1)
logger.error('Request error proxy! - Retrying in {} secs'.format(randomtime))
time.sleep(randomtime)
continue

except ssl.SSLWantWriteError:
randomtime = random.randint(0, 1)
logger.error('SSL Write Error! - Retrying in {} secs'.format(randomtime))
time.sleep(randomtime)
continue

except ssl.SSLError as err:
randomtime = random.randint(0, 1)
logger.error('SSL ERROR!\n {} - Retrying in {} secs'.format(err, randomtime))
time.sleep(randomtime)
continue

我的猜测是,如果没有收到这些错误,也许可以做类似的事情

except ConnectionRefusedError as err:
randomtime = random.randint(0, 1)
logger.error('ERROR!\n {} - Retrying in {} secs'.format(err, randomtime))
time.sleep(randomtime)
continue

except request.exceptions.ProxyError as err:
randomtime = random.randint(0, 1)
logger.error('ERROR!\n {} - Retrying in {} secs'.format(err, randomtime))
time.sleep(randomtime)
continue

我不确定,但我会很感激。任何事情将不胜感激!

最佳答案

遇到了类似的问题,尝试从内置函数中捕获它:

from builtins import ConnectionRefusedError

这似乎是最明显的解决方案,但以下方法有效:

requests.exceptions.ConnectionError

参见here

关于Python - ConnectionRefusedError、urllib3.exceptions.NewConnectionError 和 Colorama,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53452227/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com