gpt4 book ai didi

python - 请求.exceptions.ConnectionError : HTTPConnectionPool

转载 作者:太空宇宙 更新时间:2023-11-04 09:01:57 25 4
gpt4 key购买 nike

我有一个像这样的 python 脚本:

#!/usr/bin/python
import sys
import requests

if len(sys.argv) < 2:
print 'usage: python %s <file-urls>' % (sys.argv[0])
sys.exit(2)

print '%-15s %20s' % ('URL_PAGE', 'STATUS')

FileName = sys.argv[1]
InputFile = open(FileName)
OK = open('ok.txt', 'w+')
NOK = open('nok.txt', 'w+')
for url in InputFile:
status = requests.get(url).status_code
print '%-15s %15s' % (url.strip(), status)
if status == 200:
OK.write('%-15s %15s' % (url.strip(), status) + '\n')
OK.flush()
else:
NOK.write('%-15s %15s' % (url.strip(), status) + '\n')
NOK.flush()
OK.close()
NOK.close()

当我运行它时出现以下错误:

Traceback (most recent call last):
File "url-code.py", line 18, in <module>
status = requests.get(url).status_code
File "/usr/lib/python2.7/dist-packages/requests/api.py", line 55, in get
return request('get', url, **kwargs)
File "/usr/lib/python2.7/dist-packages/requests/api.py", line 44, in request
return session.request(method=method, url=url, **kwargs)
File "/usr/lib/python2.7/dist-packages/requests/sessions.py", line 383, in request
resp = self.send(prep, **send_kwargs)
File "/usr/lib/python2.7/dist-packages/requests/sessions.py", line 486, in send
r = adapter.send(request, **kwargs)
File "/usr/lib/python2.7/dist-packages/requests/adapters.py", line 378, in send
raise ConnectionError(e)
requests.exceptions.ConnectionError: HTTPConnectionPool(host='google.com%0A', port=80): Max retries exceeded with url: / (Caused by <class 'socket.gaierror'>: [Errno -2] Name or service not known)

这是什么,我该如何处理和解决它?

最佳答案

您的url 中有一个换行符; ASCII 代码点 0A 是 \n 换行符。

剥离你的输入行:

for url in InputFile:
url = url.strip()

关于python - 请求.exceptions.ConnectionError : HTTPConnectionPool,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24467557/

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