python 自动重连wifi windows的方法

yipeiwu_com6年前Python基础

如下所示:

# coding=utf-8
import urllib2
import urllib
from cookielib import CookieJar
import os
import re
import time


class ConnectWeb(object):
 def __init__(self):
  self.cookiejarinmemory = CookieJar()
  self.opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(self.cookiejarinmemory))
  urllib2.install_opener(self.opener)
  self.username = ""
  self.password = ""

 def connect_baidu(self): #检测目前是否联网
  try:
   urllib2.urlopen("http://www.baidu.com", timeout=2)
   return 1
  except:
   return 0

 def login(self): #模拟上网验证 验证网页几乎都是不同的,下面附上我们学校的, form表单自己根据情况填,用chrome很容易得到post的url和表单
  try:
   post_url = ""
   form = {"action": "login", "username": self.username, "password": self.password, "ac_id": 4,
     "user_ip": "", "nas_ip": "", "user_mac": "", "save_me": 1, "ajax": 1}
   fm1 = urllib.urlencode(form)
   page = urllib2.urlopen(post_url, fm1).read()
  except Exception as e:
   self.disconnect()
   time.sleep(1)
   self.connect_wifi()

 def disconnect(self):	# 断开wifi
  os.system("netsh wlan disconnect")

 def wifis_nearby(self):	# 查询附近wifi
  p = os.popen("netsh wlan show all")
  content = p.read().decode("GB2312", "ignore")
  temp = re.findall(u"(SSID.*\n.*Network type.*\n.*\u8eab\u4efd\u9a8c\u8bc1.*\n.*\u52a0\u5bc6.*\n.*BSSID.*\n)",
      content)
  result = []
  for i in temp:
   name = re.findall(u"SSID.*:(.*)\n", i)[0].replace(" ", "")
   result.append(name)
  return result

 def connect_wifi(self, name=None): #连接wifi
  os.system("netsh wlan connect name=%s" % name)

 def checking(self):	# 一直检测是否有断网,如果断网则重新连接
  while 1:
   try:
    if not self.connect_baidu():
     self.login()
   except:
    pass
   time.sleep(10)


if __name__ == "__main__":
 test = ConnectWeb()
 test.login()

以上这篇python 自动重连wifi windows的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python安装PIL模块时Unable to find vcvarsall.bat错误的解决方法

可能很多人遇到过这个错误,当使用setup.py安装python2.7图像处理模块PIL时,python默认会寻找电脑上以安装的vs2008.如果你没有安装vs2008,会出现Unabl...

Python 异常的捕获、异常的传递与主动抛出异常操作示例

Python 异常的捕获、异常的传递与主动抛出异常操作示例

本文实例讲述了Python 异常的捕获、异常的传递与主动抛出异常操作。分享给大家供大家参考,具体如下: 异常的捕获 demo.py(异常的捕获): try: # 提示用户输入一...

PyCharm鼠标右键不显示Run unittest的解决方法

PyCharm是一个用来写python代码的IDE,很好用。在其中建立了unittest类后,鼠标点击某个test方法后,菜单中会显示Run unittest方法。 问题描述 今天发现一...

Django中使用locals()函数的技巧

对 current_datetime 的一次赋值操作: def current_datetime(request): now = datetime.datetime.now()...

python实现发送form-data数据的方法详解

python实现发送form-data数据的方法详解

本文实例讲述了python实现发送form-data数据的方法。分享给大家供大家参考,具体如下: 源代码 -----------------------------2793612435...