faqts : Computers : Programming : Languages : Python

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

1 of 5 people (20%) answered Yes
Recently 1 of 5 people (20%) answered Yes


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

Jun 26th, 2003 13: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()