尝试 ping google.com 时的多次回溯(最近一次调用最后一次)和 FIleNotFoundError
Multiple Traceback (most recent call last) and FIleNotFoundError when trying to ping google.com
我正在尝试编写一个用于 ping ip 地址等的程序。但是每次我尝试 ping 一个 IP 地址时,我总是会遇到多个错误。我尝试了多种不同的方法来 ping 一个 IP 地址,但没有一种方法有效。任何帮助将不胜感激! :D
我的代码:
import time
import socket
import os
import subprocess
time.sleep(1)
print(' _ __ __')
time.sleep(0.3)
print(' / |/ /__ ___/ /__')
time.sleep(0.3)
print(' / / _ \/ _ / -_)')
time.sleep(0.3)
print('/_/|_/\___/\_,_/\__/')
print('\n\n')
time.sleep(1)
while True:
prompt = input('>>> ')
if prompt == '/ip-?':
print(socket.gethostbyname(socket.getfqdn()))
elif prompt == '/hn-?':
print(socket.gethostname())
elif prompt == '/h':
print('\n/ip-? --- IP ADDRESS')
print('/hn-? --- HOST NAME')
print('/e --- EXIT')
print('\n')
elif prompt == '/ip->':
#THIS IS WHERE THE PROBLEM IS OCCURING
out = subprocess.run(['ping', 'google.com'], capture_output=True)
print(out.stdout.decode())
elif prompt == '/e':
break
回溯:
Traceback (most recent call last):
File "D:\PYTHON\PROJECTS\everything.py", line 33, in <module>
out = subprocess.run(['ping', 'google.com'], capture_output=True)
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.8_3.8.1776.0_x64__qbz5n2kfra8p0\lib\subprocess.py", line 489, in run
with Popen(*popenargs, **kwargs) as process:
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.8_3.8.1776.0_x64__qbz5n2kfra8p0\lib\subprocess.py", line 854, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.8_3.8.1776.0_x64__qbz5n2kfra8p0\lib\subprocess.py", line 1307, in _execute_child
hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
FileNotFoundError: [WinError 2] The system cannot find the file specified
ping.exe
应该在系统目录下C:\WindowsSystem32
。如果没有路径 运行 失败,请添加它。您可以使用正斜杠来避免 python 使用 \
转义特殊字符的潜在问题。
out = subprocess.run(['C:/Windows/System32/ping', 'google.com'], capture_output=True)
请注意,这会使您的程序仅 windows。 Linux/Mac 也有“ping”程序,所以如果简单的“ping”不起作用,您可以将其用作后备。 “ping”程序可能有不同的参数和输出,具体取决于平台,因此您可能需要先检查平台版本,并实现两个不同版本的代码。
我正在尝试编写一个用于 ping ip 地址等的程序。但是每次我尝试 ping 一个 IP 地址时,我总是会遇到多个错误。我尝试了多种不同的方法来 ping 一个 IP 地址,但没有一种方法有效。任何帮助将不胜感激! :D
我的代码:
import time
import socket
import os
import subprocess
time.sleep(1)
print(' _ __ __')
time.sleep(0.3)
print(' / |/ /__ ___/ /__')
time.sleep(0.3)
print(' / / _ \/ _ / -_)')
time.sleep(0.3)
print('/_/|_/\___/\_,_/\__/')
print('\n\n')
time.sleep(1)
while True:
prompt = input('>>> ')
if prompt == '/ip-?':
print(socket.gethostbyname(socket.getfqdn()))
elif prompt == '/hn-?':
print(socket.gethostname())
elif prompt == '/h':
print('\n/ip-? --- IP ADDRESS')
print('/hn-? --- HOST NAME')
print('/e --- EXIT')
print('\n')
elif prompt == '/ip->':
#THIS IS WHERE THE PROBLEM IS OCCURING
out = subprocess.run(['ping', 'google.com'], capture_output=True)
print(out.stdout.decode())
elif prompt == '/e':
break
回溯:
Traceback (most recent call last):
File "D:\PYTHON\PROJECTS\everything.py", line 33, in <module>
out = subprocess.run(['ping', 'google.com'], capture_output=True)
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.8_3.8.1776.0_x64__qbz5n2kfra8p0\lib\subprocess.py", line 489, in run
with Popen(*popenargs, **kwargs) as process:
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.8_3.8.1776.0_x64__qbz5n2kfra8p0\lib\subprocess.py", line 854, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.8_3.8.1776.0_x64__qbz5n2kfra8p0\lib\subprocess.py", line 1307, in _execute_child
hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
FileNotFoundError: [WinError 2] The system cannot find the file specified
ping.exe
应该在系统目录下C:\WindowsSystem32
。如果没有路径 运行 失败,请添加它。您可以使用正斜杠来避免 python 使用 \
转义特殊字符的潜在问题。
out = subprocess.run(['C:/Windows/System32/ping', 'google.com'], capture_output=True)
请注意,这会使您的程序仅 windows。 Linux/Mac 也有“ping”程序,所以如果简单的“ping”不起作用,您可以将其用作后备。 “ping”程序可能有不同的参数和输出,具体取决于平台,因此您可能需要先检查平台版本,并实现两个不同版本的代码。