Alex Alex - 2 months ago 6
Python Question

Python Requests ignoring time outs

I'm trying to make some kind of a scanner with python (just for fun)

it will send a get request to an random ip and see if there is any answer
the problem is that every time that the connection fails the program will stop running .
this is the code

import time
import requests


ips = open("ip.txt", "r")

for ip in ips:
r = requests.get(url="http://"+ip+"/",verify=False)
print(r.status_code)
time.sleep(0.5)


this is what i get by trying just a random ip :

requests.exceptions.ConnectionError: HTTPConnectionPool(host='177.0.0.0', port=80): Max retries exceeded with url:

J F J F
Answer

This is throwing an error. To protect against this, use a try/except statement:

for ip in ips:
    try:
        r = requests.get(url="http://"+ip+"/",verify=False)
        print(r.status_code)
    except requests.exceptions.RequestException as e:
        print('Connecting to ip ' + ip + ' failed.', e) 
    time.sleep(0.5)
Comments