requests.exceptions.ConnectionError:HTTPConnectionPool

时间:2014-06-28 14:01:20

标签: python exception python-requests connectionexception

我有一个像这样的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)

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

1 个答案:

答案 0 :(得分:5)

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

剥离输入行:

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