Home > Backend Development > Python Tutorial > 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
Release: 2017-05-10 11:34:00
Original
3603 people have browsed it

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
Copy after login

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

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

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
Copy after login

[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!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template