python 自动重连wifi windows的方法

yipeiwu_com7年前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中快速进行多个字符替换的方法小结

先给出结论: 要替换的字符数量不多时,可以直接链式replace()方法进行替换,效率非常高; 如果要替换的字符数量较多,则推荐在 for 循环中调用 replace()...

wtfPython—Python中一组有趣微妙的代码【收藏】

wtfPython是github上的一个项目,作者收集了一些奇妙的Python代码片段,这些代码的输出结果会和我们想象中的不太一样; 通过探寻产生这种结果的内部原因,可以让我们对Pyth...

Python日志模块logging简介

Python日志模块logging简介

logging分为4个模块: loggers, handlers, filters, and formatters. ●loggers: 提供应用程序调用的接口 ●handlers: 把...

python 网络编程常用代码段

python 网络编程常用代码段

服务器端代码: # -*- coding: cp936 -*- import socket sock = socket.socket(socket.AF_INET, socket....

神经网络python源码分享

神经网络python源码分享

神经网络的逻辑应该都是熟知的了,在这里想说明一下交叉验证 交叉验证方法: 看图大概就能理解了,大致就是先将数据集分成K份,对这K份中每一份都取不一样的比例数据进行训练和测试。得出K个误...