Home  >  Article  >  Backend Development  >  Detailed explanation of how to implement automatic reconnection after WiFi disconnection in Python

Detailed explanation of how to implement automatic reconnection after WiFi disconnection in Python

Y2J
Y2JOriginal
2017-05-10 11:34:003550browse

To implement automatic reconnection when WiFi is disconnected, the principle is to use Python to monitor whether the network is disconnected, and restart the network service if it is disconnected. Next, I will share the implementation code with you. Friends who need it can refer to it

1.Python code autowifi.py, placed in the /home/pi directory:

#!/usr/bin/python
import
os, time
 while
True:
  if
'192' not in os.popen('ifconfig | grep 192').read():
    print
'\n****** wifi is down, restart... ******\n'
    os.system('sudo /etc/init.d/networking restart')
  time.sleep(5*60) #5 minutes

2.Shell script autowifi.sh , also placed in the /home/pi directory:

#!/bin/sh
python /home/pi/autowifi.py &

3. Automatically start the above script when booting: execute the following command in the terminal window

sudo
cp -f /home/pi/autowifi.sh /etc/init.d/
sudo
chmod +x /etc/init.d/autowifi.sh
sudo
chown root:root /etc/init.d/autowifi.sh
sudo
update-rc.d autowifi.sh default

[Related recommendations]

1. Python Free Video Tutorial

2. Python Learning Manual

3. Python Meets Data Collection Video Tutorial

The above is the detailed content of Detailed explanation of how to implement automatic reconnection after WiFi disconnection in Python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn