The script has been monitoring the network connection situation, if there is a disconnection situation, automatically connected to the network.
Not much to say, direct sticker code:
netlogin.py
1 #Coding:utf-82 3 ImportRequests4 Import Time5 Importdatetime6 ImportSYS7 8 Reload (SYS)9Sys.setdefaultencoding ("Utf-8")Ten One classNetlogin: A - def __init__(self): -Self.login_url ='Http://p.nju.edu.cn/portal_io/login' theSelf.info_url ='Http://p.nju.edu.cn/portal_io/getinfo' #log in state get, way: Get -Self.logout_url ='Http://p.nju.edu.cn/portal_io/logout' -Self.headers = { - 'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8', + 'Referer':'http://p.nju.edu.cn/', - 'user-agent':'mozilla/5.0 (Windows NT 6.3; WOW64) applewebkit/537.36 (khtml, like Gecko) chrome/43.0.2357.81 safari/537.36', + 'X-requested-with':'XMLHttpRequest' A } at -Self.form_data = { - 'username':'mgxxxxxxx', - 'Password':'xxxxxx' - } -Self.timeout = 5 in -Self.test_url ='http://www.baidu.com/' toSelf.teststr ="030173" + - the defnet_test (self): * Try: $s =requests.session ()Panax NotoginsengR = S.get (Self.test_url, Headers=self.headers, timeout=self.timeout) -pos =R.content.find (SELF.TESTSTR) the ifPOS >= 1: + #print ' Internet Ok ' A return 'Internet Ok' the Else: + #print ' Internet wrong ' - return 'Internet Wrong' $ exceptException, E: $ Printe - - the - Wuyi defcreate_session (self): thes =requests.session () - returns Wu - defLogin (self): Abouts =self.create_session () $R = S.post (Self.login_url, Self.form_data, headers=self.headers) - ifR.json () ['Reply_code'] = = 1: - PrintR.json () ['reply_msg'] - returnR.json () ['reply_msg'] A Else: + #Print U "Login Failed" the returnU"Login Failed" - $ #Get login information, need to complete login after the function is executed the defGetInfo (self): thes =self.create_session () theR = S.get (Self.info_url, headers=self.headers) the - #Network Logoff in deflogout (self): thes =self.create_session () theR = S.post (Self.logout_url, headers=self.headers) About the the if __name__=="__main__": theNET =Netlogin () +Signal =net.net_test () - #log = open (' NetLog.txt ', ' a ') the whileTrue:Bayi ifSignal = ='Internet Ok': the #s = str (Datetime.datetime.now ()) the PrintDatetime.datetime.now () - PrintSignal - " " the Log.write (s) the log.write (' \ n ') the log.write (signal) the log.write (' \ n ') - " " the the Else: the #s = str (Datetime.datetime.now ()) + ': '94 PrintSignal thestr =Net.login () the PrintDatetime.datetime.now () the PrintStr98 " " About Log.write (s) - log.write (' \ n ')101 log.write (str)102 log.write (' \ n ')103 " "104Time.sleep (1) theSignal =net.net_test ()106 " "107 num + = 1108 if num = = 5:109 flag = False the " "111 the #log.close ()
Campus Network Maintenance Script