Home     My Faqts     Contributors     About     Help    

faqts : Computers : Programming : Languages : Python

FAQTs repaired & updated!
Thanks for your patience...
Entry Add Entry Alert - Edit this Entry

Did You Find This Entry Useful?

0 of 1 people (0%) answered Yes
Recently 0 of 1 people (0%) answered Yes

How do I detect if the computer is online without opening a dial-up connection dialog (if offline)?

Jun 27th, 2003 06:00

James c359, Mickel Grönroos

I'm new to Python, so I'm not fully aware of all it can do. But this
simple ping test should work. It basicaly trys to ping the computer's
import os, sre, socket, string
def OnOffLine():
    # Gets your computers name and IP Address
    local_host_name = socket.gethostname()
    local_host_ip = socket.gethostbyname(local_host_name)
    # Splits your IP and atempts to guess your gateways IP
    remote_host = string.split(local_host_ip,".")
    remote_host = remote_host[:-1]
    remote_host_ip = ''
    for item in remote_host:
        remote_host_ip = remote_host_ip+item+'.'
    remote_host_ip = remote_host_ip+"1"
    # Pings your gateway
    ping = os.popen("ping "+remote_host_ip)
    data = ping.read()
    # Searches for the number of times your hosts IP occurs and guesses
if your online or not
    ping_count = sre.findall(remote_host_ip,data)
    if len(ping_count) > 2:
        return "Online"
        return "Offline"
print OnOffLine()

© 1999-2004 Synop Pty Ltd