I want to create a python code, little code with ping test, and send the result to the Flask server in order to print a result into a html page.
I’m not sure this is the best way, but I try to do that :
Python script :
import os, sys, time
import requests
obj = {'somekey': 'somevalue'}
pc_on = ['http://localhost:5500/pc_1_on','http://localhost:5500/pc_2_on']
pc_off = ['http://localhost:5500/pc_1_off','http://localhost:5500/pc_2_off']
pc_cosim = {1:"10.24.135.2", 2:"10.24.135.3"}
while True:
for value in pc_cosim.items():
addr=f"{value[1]}"
position=value[0]-1
reponse1 = os.system("ping -n 1 " + addr + " -w 1")
print(reponse1)
if (reponse1 == 0):
try:
resp = requests.post(pc_off[position], data = obj, timeout=2)
except:
print("exception active")
if (reponse1 > 0):
try:
resp = requests.post(pc_on[position], data = obj, timeout=2)
except:
print("exception inactive")
time.sleep(2)
And into the Flask server python code, I want to catch the request message with this code and print into the server cmd window some info. Flask server code is like that :
from flask import Flask, render_template, request
from flask_socketio import SocketIO, emit
from flask_cors import CORS
import os, time
from datetime import datetime
from flask import Flask, render_template
now = datetime.now()
app = Flask(__name__, template_folder='templates', static_folder='staticFiles')
app.config['SECRET_KEY'] = 'secret!'
socketio = SocketIO(app)
global state_disable, state_enable, state_stop, state_reboot, state_start, state_cleanup, lines, pingpc_1, pingpc_2, pingpc_3, pingpc_4, pingpc_5, pingpc_6
state_disable = 0
state_enable = 0
state_stop = 0
state_reboot = 0
state_start = 0
state_cleanup = 0
pingpc_1 = 1 # ici la valeur 1 indique que le status est OFF
pingpc_2 = 1
pingpc_3 = 1
pingpc_4 = 1
pingpc_5 = 1
pingpc_6 = 1
@socketio.on('pc_1_on')
def handle_message(data):
print("socket ping_pc_1 received")
print(data)
if (ping=="1"):
print("message name pc_off1 received", data)
emit('ping_pc_1','server received your message' + data)
print("ping emit")
if (pc_1=="on"):
print("message name pc_on1 received", data)
emit('ping_pc_1','server received your message' + data)
emit('ping_pc_1',pingpc_1)
print("ping emit")
I have no return from the server cmd console. I’m certain the request from the python code is uncorrect, no idea why. If you have some advises to give.
Thank you so much.