简体   繁体   中英

Max retries exceeded with URL Error on using requests.get()

I'm trying to write a script in python to send HTTP get request to automatically generated URLs and get its response code and elapsed time. The URLs need not necessarily be a valid one, 400 responses are acceptable too.

script1.py

import sys
import requests

str1="http://www.googl"
str3=".com"
str2='a'

for x in range(0, 8):
    y = chr(ord(str2)+x)
    str_s=str1+y+str3
    r=requests.get(str_s)
    print(str_s, r.status_code, r.elapsed.total_seconds())

Error:

File "script1.py", line 12, in <module><br>
    r=requests.get(str_s)<br>
  File "/usr/local/lib/python2.7/dist-packages/requests/api.py", line 72, in get<br>
    return request('get', url, params=params, **kwargs)<br>
  File "/usr/local/lib/python2.7/dist-packages/requests/api.py", line 58, in request<br>
    return session.request(method=method, url=url, **kwargs)<br>
  File "/usr/local/lib/python2.7/dist-packages/requests/sessions.py", line 508, in request<br>
    resp = self.send(prep, **send_kwargs)<br>
  File "/usr/local/lib/python2.7/dist-packages/requests/sessions.py", line 618, in send<br>
    r = adapter.send(request, **kwargs)<br>
  File "/usr/local/lib/python2.7/dist-packages/requests/adapters.py", line 508, in send<br>
    raise ConnectionError(e, request=request)<br>
requests.exceptions.ConnectionError: HTTPConnectionPool(host='www.googla.com', port=80): Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7fc44c891e50>: Failed to establish a new connection: [Errno -2] Name or service not known',))
  • I just want see the time taken to receive response of each request.
  • Only one request has to be sent
  • Response code does not matter.

I guess you want to get something like this:

import sys
import requests

str1="http://www.googl"
str3=".com"
str2='a'

for x in range(0, 8):
    y = chr(ord(str2)+x)
    str_s=str1+y+str3
    print('Connecting to ' + str_s)
    try:
        r = requests.get(str_s)
        print(str_s, r.status_code, r.elapsed.total_seconds())
    except requests.ConnectionError as e:
        print("  Failed to open url")

In this case, using the try...except you can catch the exception that get raises and handle it in a nice way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM