Python - Detect if remote computer is on
Asked Answered
D

5

10

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!

Diuretic answered 16/7, 2013 at 17:6 Comment(3)
I think your idea is on the right track, what have you tried so far?Starvation
Either the title is misleading or you just want to detect if your application is running. If later then any type of pinging will do.Lissettelissi
I agree w/ @Ma3x, this question really does not have much to do w/ Python.Eyeglasses
M
23

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

try:
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
        s.connect(("hostname", 22))
    print("Port 22 reachable")
except OSError as e:
    print("Error on connect: %s" % e)

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

Megganmeggi answered 16/7, 2013 at 17:44 Comment(0)
S
4

First you should know there is no standard way to determine if a computer is ON or not, and this is not language/platform dependent. A computer can fake any state you try to check.

But the usual way to check if a computer is ON is sending a Ping (ICMP Echo Request). This answer shows how to send a Ping using Python.

Scofield answered 16/7, 2013 at 17:16 Comment(0)
B
0

It seems like what you're looking for is a network/host monitoring tool, or just a simple heart beat monitor. Depending on the specifics (like number of hosts to monitor), something like Nagios, Munin or Heartbeat might help.

Bailly answered 16/7, 2013 at 17:13 Comment(0)
S
0

yes thats the way to go. kind of like sending heartbeat ping. Since its UDP and since its just a header message you can reduce the frequency to say 10 seconds. This should not cause any measurable system perf degradation since its just 2 systems we are talking about.

I feel here, UDP is might be better compared to TCP. Its lightweight, does not consume a lot of system resources and is theoretically faster. Downside would be there could be packet loss. You can circumvent that by putting in some logic like when 10 packets (spaced 10 seconds apart) are not received consecutively then declare the other system unreachable.

Stearne answered 16/7, 2013 at 17:14 Comment(1)
Hey that was my answer :)Hygro
F
0

Add a web server to your app.

http://www.tornadoweb.org/en/stable/ http://webpy.org/

Fatimafatimah answered 16/7, 2013 at 17:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.