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]
#6
(Jun-27-2022, 07:16 AM)ibreeden Wrote: No. You must read the ip addresses from the json file one by one, and for each address test if it responds.
from subprocess import call, DEVNULL
import platform
import json
import os


def folder():
    path = os.getcwd()
    newPath = path + "/tmp"
    os.mkdir(newPath)

def ping(host_or_ip: str) -> bool :
    """ping(): 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)

responding_hosts = 0
with open("clients.json") as f:
    data = json.load(f)
    for i in data:
        print(f"pinging: {i['ip']}")
        if ping(i["ip"]):
            responding_hosts += 1

print(f"Number of responding hosts: {responding_hosts}")
if responding_hosts > 0 :
    folder()

Okay nice, this worked great!
Sorry, but I have one last question. I tried to check for two different json files (one contains a list of IPs which should be offline and one which should be online) but I'm running into some problems once again:

#!/usr/bin/env python3

#Imports
from subprocess import call, DEVNULL
import platform
import json
import os

#IP Configuration
responding_clients1 = 0
responding_clients2 = 0

#Folder Creation
def folder():
    path = os.getcwd()
    newPath = path + "/tmp"
    os.mkdir(newPath)

#Pinp Configuration
def ping(host_or_ip: str) -> bool :
    if platform.system().lower() == "windows":
        countoption = "/n"
    else:
        countoption = "-c"
    return not call(["ping", countoption, "1", host_or_ip],
                    stdout=DEVNULL, stderr=DEVNULL)

#Clients1 Check
with open("clients1.json") as f1:
    data1 = json.load(f1)
    for i in data1:
        if ping(i["ip"]):
            responding_clients1 += 1

#Clients2 Check
with open("clients2.json") as f2:
    data2 = json.load(f2)
    for i in data2:
        if ping(i["ip"]):
            responding_clients2 += 1

#ToDo
if responding_clients1 > 0 :
    if responding_clients2 == 0:
        folder()

#End
sys.exit()
And I get this error message:

Traceback (most recent call last):
  File "/home/pi/system/debug/ipCheck/v1.0.py", line 37, in <module>
    data2 = json.load(f2)
  File "/usr/local/lib/python3.10/json/__init__.py", line 293, in load
    return loads(fp.read(),
  File "/usr/local/lib/python3.10/json/__init__.py", line 346, in loads
    return _default_decoder.decode(s)
  File "/usr/local/lib/python3.10/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/local/lib/python3.10/json/decoder.py", line 353, in raw_decode
    obj, end = self.scan_once(s, idx)
json.decoder.JSONDecodeError: Expecting ',' delimiter: line 9 column 3 (char 112)
Reply


Messages In This Thread
RE: Check if clients are online with ips stored in json - by AlphaInc - Jun-27-2022, 08:02 AM

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