我试着创建了一个小应用,当你长时间断开连接时,它会播放一个声音,当连接建立时,它会播放另一个声音。适用于无线连接。
我仍然是Python的新手:)尝试小项目来提高我的知识。如果你真的回答了,如果你能包括任何关于如何使用子进程的信息,我将非常感激。
我已经定义子过程,但我不确定如何表达我的if语句,所以它从一个函数循环到另一个函数。IE功能1=如果ping丢失> 15次ping,则播放声音并转到功能2...如果功能2 ping成功> 15次,则播放声音并返回功能1。以此类推。
我还没有将程序包装在循环中,此时我只是尝试让ping与if语句一起工作。
因此,现在应用程序只是不断地循环ping。
import os
import subprocess
import winsound
import time
def NetFail():
winsound.Beep(2000 , 180), winsound.Beep(1400 , 180)
def NetSucc():
winsound.Beep(1400 , 250), winsound.Beep(2000 , 250),
ips=[]
n = 1
NetSuccess = 10
NetFailure = 10
PinSuc = 0
PinFail = 0
x = '8.8.8.8'
ips.append(x)
for ping in range(0,n):
ipd=ips[ping]
def PingFailure():
while PinFail < NetSuccess:
res = subprocess.call(['ping', '-n', '10', ipd])
if ipd in str(res):
PingSuccess()
else:
print ("ping to", ipd, "failed!"), NetFail()
def PingSuccess():
while PinFail < NetFailure: # This needs to be cleaned up so it doesn't interfere with the other function
res = subprocess.call(['ping', '-n', '10', ipd])
if ipd in str(res):
PingFail()
else:
print ("ping to", ipd, "successful!"), NetSucc()
发布于 2015-12-09 00:47:06
当您使用ping -n 10 ip
命令时,我假设您使用的是Windows系统,因为在Linux (或其他类Unix)上,它应该是ping -c 10 ip
。
不幸的是,在Windows上,ping
总是返回0,所以您不能使用返回值来知道是否到达了peer。甚至输出也不是很清楚。
所以你应该:
cmd
控制台中使用可访问和不可访问的ip运行命令ping -n 1 ip
,记录输出并找出其中的差异。在我的(法语)系统上,它写着不可能,我想在你的区域设置中,你应该得到Unable或者等同于从Python启动ping
subprocess.Popen
从命令中获取输出(和错误输出)。
代码可能类似于:
errWord = 'Unable' # replace with what your locale defines...
p = subprocess.Popen([ 'ping', '-n', '1', ipd],
stdout = subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate()
if errWord in out:
# process network disconnected
else:
# process network connected
或者,您可以在pypi中搜索ping的纯Python实现,如py-ping ...
无论如何,我不会在flip-flop中使用两个函数,因为如果您稍后想要测试到多个IP的连通性,这将更加困难。我更愿意使用一个类
class IP(object):
UNABLE = "Unable" # word indicating unreachable host
MAX = 15 # number of success/failure to record new state
def __init__(self, ip, failfunc, succfunc, initial = True):
self.ip = ip
self.failfunc = failfunc # to warn of a disconnection
self.succfunc = succfunc # to warn of a connection
self.connected = initial # start by default in connected state
self.curr = 0 # number of successive alternate states
def test(self):
p = subprocess.Popen([ 'ping', '-n', '1', self.ip],
stdout = subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate()
if self.UNABLE in out:
if self.connected:
self.curr += 1
else:
self.curr = 0 # reset count
else:
if not self.connected:
self.curr += 1
else:
self.curr = 0 # reset count
if self.curr >= self.MAX: # state has changed
self.connected = not self.connected
self.curr = 0
if self.connected: # warn for new state
self.succfunc(self)
else:
self.failfunc(self)
然后,您可以遍历IP对象列表,反复调用ip.test()
,并且会收到状态更改的警告
发布于 2015-12-08 22:49:27
不太确定您想要实现什么,但是如果您希望在每次通过子进程调用ping时都执行if语句,那么if语句必须是while循环的一部分。
另外:下面是subprocess:https://docs.python.org/3/library/subprocess.html的文档,用于查看您必须通过subprocess.call_output调用的进程的输出:
ls_output = subprocess.check_output(['ls'])
有关更多信息,请查看以下内容:http://sharats.me/the-ever-useful-and-neat-subprocess-module.html#a-simple-usage
https://stackoverflow.com/questions/34158720
复制相似问题