Saturday, 15 May 2010

Python Function to test ping -



Python Function to test ping -

i'm trying create function can phone call on timed basis check ping , homecoming result can update on-screen display. new python don't understand how homecoming value or set variable in function.

here code works:

import os hostname = "google.com" response = os.system("ping -c 1 " + hostname) if response == 0: pingstatus = "network active" else: pingstatus = "network error"

here effort @ creating function:

def check_ping(): hostname = "google.com" response = os.system("ping -c 1 " + hostname) # , check response... if response == 0: pingstatus = "network active" else: pingstatus = "network error"

and here how display pingstatus:

label = font_status.render("%s" % pingstatus, 1, (0,0,0))

so looking how homecoming pingstatus function. help appreciated.

it looks want return keyword

def check_ping(): hostname = "taylor" response = os.system("ping -c 1 " + hostname) # , check response... if response == 0: pingstatus = "network active" else: pingstatus = "network error" homecoming pingstatus

edit:

like @poke said in comments, need capture/'receive' homecoming value of function(pingstatus) in variable like:

pingstatus = check_ping()

some info on python functions:

http://www.tutorialspoint.com/python/python_functions.htm

http://www.learnpython.org/en/functions

it's worth going through introductory tutorial python, cover fundamentals. recommend investigating udacity.com , codeacademy.com

python function variables return

No comments:

Post a Comment