이런 코드가 있는데요.
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)
실행해보면 아래와 같은 에러가 생기는데요.
Request timed out Request timed out ....
Request timed out
Traceback (most recent call last): File "C:\Users\test\Documents\test.py", line 17, in ping(h, verbose=True) File "C:\Users\test\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\test\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\test\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\test\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
python에서 에러가 생겨도 계속 실행하는 방법이 있나요?
python
아래처럼 try catch를 쓰면 됩니다.
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")
© 2022 pinfo. All rights reserved.