mardi 8 décembre 2015

if statement for a subprocess python not working

I've tried to create a little app that plays a sound when you lose connectivity for an extended period and plays another when the connection is established. Useful for wireless connections.

I'm still new to Python :) trying little projects to improve my knowledge. If you do answer I will be very grateful if you could include any information about how to use subprocess.

I've defined the subprocess but I'm not sure how to word my if statement so it loops from one function to the other. IE Function 1 = IF ping loss > 15 pings play sound and move on to function 2... If function 2 ping success > 15 pings play sound and move back to function 1. So on.

I've yet to wrap the program in a loop, at this point I'm just trying to get the ping to work with the if statement.

So right now the application just continuously loop pings.

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

Aucun commentaire:

Enregistrer un commentaire