0

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: 

1 Answer 1

2

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) 
Sign up to request clarification or add additional context in comments.

5 Comments

catching requests.exceptions.RequestException would capture more probems.
Nitpick: Adding from requests.exceptions import RequestConnection, ConnectionError would be better for readability.
@IanAuld - not necessarily. Discarding the namespace leaves future readers guessing which one you are talking about.
ConnectionError inherits from RequestException so you only need the more general one.
Thanks again @tdelaney

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.