wifi-status-checker.py

Raspberry Pi Smart Display Module · File : wifi-status-checker.py · Language : Python · Use case : Vérifier la connectivité internet et afficher l'adresse IP locale pour le diagnostic réseau.

Network diagnostic script using socket and requests libraries to test connectivity.

Works on Raspberry Pi Smart Display Module with CM5 connected via Wi-Fi or Ethernet. Install the requests library via 'pip install requests'. The script determines the local IP by opening a UDP socket to 8.8.8.8 without sending data. It then tests access to an external website to confirm Internet access.

wifi-status-checker.py

import socket
import subprocess
import requests

def get_local_ip():
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    try:
        s.connect(('8.8.8.8', 80))
        ip = s.getsockname()[0]
    except Exception:
        ip = '127.0.0.1'
    finally:
        s.close()
    return ip

def check_internet():
    try:
        requests.get('http://google.com', timeout=5)
        return True
    except requests.ConnectionError:
        return False

if __name__ == "__main__":
    print(f"IP Locale: {get_local_ip()}")
    if check_internet():
        print("Connexion Internet : OK")
    else:
        print("Connexion Internet : Échec")

← Back to the board