How to get python code to keep running even if error occurs
Question:
the code that i currently use
from pythonping import ping
import random
while 1:
d1 = (random.randrange(1,255))
d2 = (random.randrange(1,255))
d3 = (random.randrange(1,255))
d4 = (random.randrange(1,255))
h = f'{d1}.{d2}.{d3}.{d4}'
ping(h, verbose=True)
but when an error happens(below) it ends the program how can i keep it going
Request timed out Request timed out ….
Request timed out
Traceback (most recent call last): File
"C:UsersdwatnDocumentsdocument1.py", line 17, in
ping(h, verbose=True) File "C:UsersdwatnAppDataLocalPackagesPythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0LocalCachelocal-packagesPython39site-packagespythonping_init_.py",
line 78, in ping
comm.run(match_payloads=match) File "C:UsersdwatnAppDataLocalPackagesPythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0LocalCachelocal-packagesPython39site-packagespythonpingexecutor.py",
line 335, in run
payload_bytes_sent = self.send_ping(identifier, seq, payload) File
"C:UsersdwatnAppDataLocalPackagesPythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0LocalCachelocal-packagesPython39site-packagespythonpingexecutor.py",
line 277, in send_ping
self.socket.send(i.packet) File "C:UsersdwatnAppDataLocalPackagesPythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0LocalCachelocal-packagesPython39site-packagespythonpingnetwork.py",
line 56, in send
self.socket.sendto(packet, (self.destination, 0)) OSError: [WinError 10051] A socket operation was attempted to an unreachable
network
Answers:
You can use try catch to handle exceptions.
from pythonping import ping
import random
while 1:
d1 = (random.randrange(1,255))
d2 = (random.randrange(1,255))
d3 = (random.randrange(1,255))
d4 = (random.randrange(1,255))
h = f'{d1}.{d2}.{d3}.{d4}'
try:
ping(h, verbose=True)
except:
print("invalid ip")
the code that i currently use
from pythonping import ping
import random
while 1:
d1 = (random.randrange(1,255))
d2 = (random.randrange(1,255))
d3 = (random.randrange(1,255))
d4 = (random.randrange(1,255))
h = f'{d1}.{d2}.{d3}.{d4}'
ping(h, verbose=True)
but when an error happens(below) it ends the program how can i keep it going
Request timed out Request timed out ….
Request timed out
Traceback (most recent call last): File
"C:UsersdwatnDocumentsdocument1.py", line 17, in
ping(h, verbose=True) File "C:UsersdwatnAppDataLocalPackagesPythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0LocalCachelocal-packagesPython39site-packagespythonping_init_.py",
line 78, in ping
comm.run(match_payloads=match) File "C:UsersdwatnAppDataLocalPackagesPythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0LocalCachelocal-packagesPython39site-packagespythonpingexecutor.py",
line 335, in run
payload_bytes_sent = self.send_ping(identifier, seq, payload) File
"C:UsersdwatnAppDataLocalPackagesPythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0LocalCachelocal-packagesPython39site-packagespythonpingexecutor.py",
line 277, in send_ping
self.socket.send(i.packet) File "C:UsersdwatnAppDataLocalPackagesPythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0LocalCachelocal-packagesPython39site-packagespythonpingnetwork.py",
line 56, in send
self.socket.sendto(packet, (self.destination, 0)) OSError: [WinError 10051] A socket operation was attempted to an unreachable
network
You can use try catch to handle exceptions.
from pythonping import ping
import random
while 1:
d1 = (random.randrange(1,255))
d2 = (random.randrange(1,255))
d3 = (random.randrange(1,255))
d4 = (random.randrange(1,255))
h = f'{d1}.{d2}.{d3}.{d4}'
try:
ping(h, verbose=True)
except:
print("invalid ip")