37 lines
996 B
Python
37 lines
996 B
Python
import socket
|
|
import asyncio
|
|
from flask import Flask, render_template
|
|
|
|
app = Flask(__name__)
|
|
|
|
async def scan_for_pi() -> dict():
|
|
ip = socket.gethostbyname(socket.gethostname())
|
|
baseIP = '.'.join(ip.split(".")[:3])
|
|
|
|
def scan(ip: str) -> str:
|
|
try:
|
|
with socket.socket() as s:
|
|
s.settimeout(0.5)
|
|
s.connect((ip, 5000))
|
|
s.send("PING".encode())
|
|
data = s.recv(1024).decode()
|
|
if data == "PONG":
|
|
print("found:", ip)
|
|
return ip
|
|
except OSError:
|
|
pass
|
|
|
|
async def scan_async(ip: str) -> str:
|
|
return await asyncio.to_thread(scan, ip)
|
|
|
|
pi_list = await asyncio.gather(*[scan_async(baseIP + "." + str(i)) for i in range(255)])
|
|
return [pi for pi in pi_list if pi is not None]
|
|
|
|
@app.route('/')
|
|
def home():
|
|
return render_template('index.html')
|
|
|
|
@app.route('/set/<int:color>')
|
|
def set(color):
|
|
return f'{color}'
|