Python Forum
Check if clients are online with ips stored in json [SOLVED]
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Check if clients are online with ips stored in json [SOLVED]
#2
Hi @AlphaInc ,
I understand you have two problems:
  1. You need to read a JSON file,
  2. You need to know if an IP address responds.

You dit almost right reading the JSON file, but you missed a detail. Look at the file: it starts with "[" so it is a list. There is one comma, so the list contains two items. The items start with "{" so they are dictionaries. You want the IP-address, and you see these addresses have key "ip".
So to extract these adresses you should do someting like:
import json

with open("clients.json") as f:
    data = json.load(f)
    for i in data:
        print(i["ip"])
Now for the ping(). You could use pythonping, but if I understand the manual right, you need to be superuser to run a script using pythonping. I consider this to be bad practice.
So you need to use the default ping on your computer. On Windows you should execute one ping with "ping /n 1 192.168.1.2". On Linux-like systems you need to use "-c" (count) instead of "/n". So you could define a function like this:
from subprocess import call, DEVNULL
import platform

def ping(host_or_ip: str) -> bool :
    """ping() executes:
    on windows:
        ping /n 1 host_or_ip
    on Linux or Mac:
        ping -c 1 host_or_ip
    Meaning: execute one ping to host_or_ip.
    Returns True if host_or_ip responded,
    else False.
    """
    if platform.system().lower() == "windows":
        countoption = "/n"
    else:
        countoption = "-c"
    return not call(["ping", countoption, "1", host_or_ip],
                    stdout=DEVNULL, stderr=DEVNULL)
Please show us how you use these building blocks to create your program. Let us know if you run into troubles.
Reply


Messages In This Thread
RE: Check if clients are online with ips stored in json - by ibreeden - Jun-26-2022, 05:15 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [solved] list content check paul18fr 6 796 Jan-04-2024, 11:32 AM
Last Post: deanhystad
  Loop through json file and reset values [SOLVED] AlphaInc 2 2,242 Apr-06-2023, 11:15 AM
Last Post: AlphaInc
  Response.json list indices must be integers or slices, not str [SOLVED] AlphaInc 4 6,570 Mar-24-2023, 08:34 AM
Last Post: fullytotal
  [SOLVED] [sqilte3] Check if column not empty? Winfried 5 1,179 Jan-28-2023, 12:53 PM
Last Post: Winfried
  Stream via socket with multiple clients principemestizo 0 1,544 Nov-01-2021, 06:25 PM
Last Post: principemestizo
  Duplex Named Pipe with Python Server and C# Clients raybowman 1 2,439 Dec-03-2020, 09:58 PM
Last Post: Gribouillis
  Saving and accessing data from different clients SheeppOSU 1 1,992 Jul-28-2019, 02:15 AM
Last Post: metulburr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020