Python - Detect if remote computer is on

jndok picture jndok · Jul 16, 2013 · Viewed 19.7k times · Source

So, I have an application in Python, but I want to know if the computer (which is running the application) is on, from another remote computer.

Is there any way to do this? I was thinking to use UDP packets, to send some sort of keep-alive, using a counter. Ex. every 5 mins the client sends an UDP 'keep-alive' packet to the server. Thanks in advance!

Answer

Dr. Jan-Philip Gehrcke picture Dr. Jan-Philip Gehrcke · Jul 16, 2013

If your goal actually is to test whether a specific service is running on the remote machine, you could test if the network port that this service should run on is reachable. Example:

import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
    s.connect(('hostname', 22))
    print "Port 22 reachable"
except socket.error as e:
    print "Error on connect: %s" % e
s.close()

If the application you want to test for is designed to run on e.g. port 1337, then check this port.