PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, October 22, 2022

[FIXED] How to get python code to keep running even if error occurs

 October 22, 2022     ip, ping, python, sockets     No comments   

Issue

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:\Users\dwatn\Documents\document1.py", line 17, in ping(h, verbose=True) File "C:\Users\dwatn\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\pythonping_init_.py", line 78, in ping comm.run(match_payloads=match) File "C:\Users\dwatn\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\pythonping\executor.py", line 335, in run payload_bytes_sent = self.send_ping(identifier, seq, payload) File "C:\Users\dwatn\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\pythonping\executor.py", line 277, in send_ping self.socket.send(i.packet) File "C:\Users\dwatn\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\pythonping\network.py", line 56, in send self.socket.sendto(packet, (self.destination, 0)) OSError: [WinError 10051] A socket operation was attempted to an unreachable network


Solution

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")


Answered By - Shafqat Jamil Khan
Answer Checked By - David Marino (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing