requests.exceptions.ConnectionError: HTTPConnectionPool

MLSC picture MLSC · Jun 28, 2014 · Viewed 16.5k times · Source

I have a python script like so:

#!/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()

When I run it the following error accurs:

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)

What is it and how can I deal with it and resolve it?

Answer

Martijn Pieters picture Martijn Pieters · Jun 28, 2014

You have a newline in your url; ASCII codepoint 0A is the \n newline character.

Strip your input lines:

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