The School campus network connection has free connection and the charge connection two types, can imagine the fee connection browsing experience is better, for example may visit more website. Before the billing address can only open a monthly service to use, and then incredibly have 60 hours per month free use of the discount address. However, once the billing address is connected and forget to disconnect in a timely manner, 60 hours will be exhausted soon.
In order to save the usage time of the toll address, the following scheme is used: Every 1 hours, it is disconnected from the campus network at the same time, connected with free connection, so that after each manual connection charge connection, up to 1 hours will be automatically disconnected.
1. Python enables connection/disconnection of network Connections
Implement the Python script to access the its.pku.edu.cn and set the connection/disconnect.
Through the Fiddler capture package, analysis from Access to its.pku.edu.cn, to fill in the account password, and then to the last successful login process, browser and gateway interaction HTTP request and response.
Discover:
(1) Visit http://its.pku.edu.cn
Request such as:
The gateway response is as follows:
Note the %7C%3BkiDrqvfi7d%24v0p5Fg72Vwbv2%3B%7C
string in which the string is used in the subsequent post.
(2) Fill in the account password, connect to the addresshttp://its.pku.edu.cn/cas/login
, the text in the request is:
Discover that it contains accounts and passwords, and&pwd_t=%E5%AF%86%E7%A0%81
(This is a fixed character), and the string%7C%3BkiDrqvfi7d%24v0p5Fg72Vwbv2%3B%7C
(This was obtained from the last response).
...
The intermediate browser also connects
http://its.pku.edu.cn/netportal/ 
http://its.pku.edu.cn/ Netportal/netportal_utf-8.jsp 
http://its.pku.edu.cn/netportal/ Blank_utf-8.html 
http://its.pku.edu.cn/connect.htm
URLs, which, through single-step tracking, have no effect on the success of the final network connection and are not considered.
...
(3) Set the connection operation, the browser is connected to http://its.pku.edu.cn/netportal/ipgwopen?sid=449
Now you need to know how 449 came from the previous browser and the gateway between the interaction between the several response did not find 449, consider 449 may be generated by the function. Found in the http://its.pku.edu.cn/netportal/js/funcs_v2.js?509173
response of the URL
function Doaction3 (_d) { if (_d.indexof ("?")! =-1) { window.frames[' mycontent '].location.href=_d+ "&sid=" +math.floor (Math.random () *1000); } else{ window.frames[' mycontent '].location.href=_d+ "? sid=" +math.floor (Math.random () *1000);} }
JS function, the original 449 is a randomly generated value.
Notice that http://its.pku.edu.cn/netportal/ipgwopen?sid=449
in the Ipgwopen, this is the type representation of the connection, through http://its.pku.edu.cn/netportal/netportal_UTF-8.jsp
the analysis of the response content can be found:
ipgwopen 表示连接免费地址
ipgwopenall 表示连接免费和收费地址
ipgwclose 表示断开本机连接
ipgwcloseall 表示断开所有连接
Step (3) after the discovery can be normal Internet, so when using the Python simulation browser login gateway, only need to simulate 1-3 of these three steps, you need to be careful to get the string in step 1 response, and in step 2 to post to the gateway, And the SID of Step 3 is a randomly generated integer.
Python code implementation
源代码使用python3 实现,因此需要机器安装 python3 环境。
#!/usr/bin/env python3 "python crawler" #encoding: Utf-8import reimport osimport gzipimport randomimport sysimport Timeimport urllibimport urllib.requestimport http.cookiejarimport http.clientdef MakeOpener (head = {' Connetio N ': ' keep-alive ', ' Accept ': ' text/html, Application/xhtml+xml, */* ', ' accept-language ': ' E n-us, en;q=0.8,zh-hans-cn;q=0.5,zh-hans;q=0.3 ', ' user-agent ': ' mozilla/5.0 (Windows NT 6.3; Win64; x64; rv:39.0) gecko/20100101 firefox/39.0 '): CJ = Http.cookiejar.CookieJar () Pro = Urllib.request.HTTPCookieProcessor (CJ ) #Python自动处理cookie opener = Urllib.request.build_opener (pro) #用于打开url的opener # urllib.request.install_opener (Open ER) Setopenerheader (opener, head) #构造http头部数据, imitate a browser to access the URL return opener# constructs opener header information, the parameter header is A dictionary, as above ' connetion ': ' keep-alive ', ' Accept ': ' text/html, Application/xhtml+xml, */* ', #将字典中的key和value对, into tuples (k,v) Then add the Def setopenerhead to the Addheaders list in openerER (opener, header): Tmp_header = [] for key, value in Header.items (): Elem = (key, value) TMP_HEADER.A Ppend (elem) opener.addheaders = tmp_header#http The data returned may be compressed, if the page is compressed, unzip. The resulting data is a byte array, which is then parsed by the character encoding of the Web page to translate the byte array into a string def decodepagedata (data): Try:data = gzip.decompress (data) exce pt:# print (' exception occurs when decompress as gzip ') pass result = Data Encode_options = [' Utf-8 ', ' GBK ', ' gb2312 '] for encode_op in encode_options: #尝试多种编码的解析, translates the data returned by HTTP (a byte array) to a string try:result = Data.decode (encode_op) except:continue else:break return resultdef getencodedpost Data (page_data, username1, password, FREE_GW = True): #获取服务端进行验证的一个字符串, by contrast in Fiddler, you need to add this validation string to the next post in the local CER = Re.compile (R ' \ (\ "username\" \) \.value\+unescape\ (\ "(. +) \" \) \+ ') Encode_str = ' for M in Cer.finditer (page_data): Encode_str = M.group (1) Break if not encode_str: Print (' Parse username generating formula failed ... ') return result = ' username1= ' + username1 result + = ' &am P;password= ' result + = password result + = ' &pwd_t=%e5%af%86%e7%a0%81 ' result + = ' &fwrd= ' if FREE_GW: Result + = ' free ' else:result + = ' fee ' result + = ' &username= ' result + username1 res Ult + = encode_str result + Password result + = Encode_str if free_gw:result + = ' "Else:resul T + = ' One ' return result# performs different operations according to option, USER_NAME and passwd for campus network account and password def pkugatewayoperation (option, user_name = ' 14012 14230 ', passwd = ' xxxxx '): #伪造的一个浏览器访问url的http头部 header = {' Connection ': ' keep-alive ', ' acce PT ': ' */* ', ' accept-language ': ' en-us, en;q=0.8,zh-hans-cn;q=0.5,zh-hans;q=0.3 ', ' user-agent ': ' Mozi lla/5.0 (Windows NT 6.3; Win64; x64; rv:39.0) gecko/20100101 firefox/39.0 ', ' accept-encoding ': ' gzip, deflate ', ' Host ': ' ItS.pku.edu.cn '} #第一步, analog browser login Http://its.pku.edu.cn/url = ' http://its.pku.edu.cn/' opener = Makeopene R (header) op = opener.open (URL) data = Decodepagedata (Op.read ()) header[' Referer '] = ' http://its.pku.edu.cn/' url = ' Http://its.pku.edu.cn/cas/login ' Setopenerheader (opener, header) FREE_GW = True if (option = = 2): FREE_GW = False #根据返回的网页数据, set post data, mainly user name, password, connection mode post_data = Getencodedpostdata (data, user_name, passwd, free_ GW) #第二步, post user name and password, in Post_data, which also adds some validation information (obtained from the last response) op = Opener.open (URL, Post_data.encode (Encodin g= ' Utf-8 ')) option_str = ' ipgwopen ' if option = = 2:option_str = ' ipgwopenall ' elif option = = 3:op Tion_str = ' ipgwclose ' elif option = = 4:option_str = ' Ipgwcloseall ' #添加操作选项 and a random number! url = ' http://its.pku.edu.cn/netportal/' + option_str + '? sid= ' + str (random.randint (1,999)) #第三部, set connection operation op = opener . Open (URL) #命令行参数, to set different actions # # connect free URL # # connection toll URL #3 disconnecting the Native connection # # disconnect all connections # # Disconnect the native connection, and then reconnect the free address (this is to set the program to run every 1 hours to prevent the billing address from connecting for too long.) Time off the network, save the use of the billing address) if __name__ = = ' __main__ ': If Len (sys.argv)! = 2:print (' Usage <option:1 (connect free), 2 (Connect global), 3 (Disconnect this computer), 4 (disconnect All), 5 (Disconnect the computer and connect free) Exit (1) option = Int (sys.argv[1]) if option < 1 or option > 5:print (' Invalid option number: ' + sys.ar GV[1]) print (' Usage <option:1 (connect free), 2 (Connect global), 3 (Disconnect this computer), 4 (Disconnect a ll), 5 (Disconnect this computer and connect free) ' exit (1) if option = = 5:pkugatewayoperation (3) Pkugatewayoperation (1) fb = open (' G:\\workspace\\mytools\\python\\log.txt ', ' a ') fb.write (' This pro Gram runs at ' + time.strftime ('%y-%m-%d%h:%m:%s ', Time.localtime (Time.time ()))) Fb.close () Else:pkugat Ewayoperation (option)
2. Automatic execution every 1 hours
Linux is more convenient, directly set crontab can be.
Under Windows, you can set up automatic task scheduling for your machine, with the following steps:
(1) Start and all tools->windows management tools, Task Scheduler
(2) Create a task
(2.1) General--fill in the program name (optional)
(2.2) Trigger-new, set to start daily fixed time, repeat every one hours
(2.3) Action-new, set to execute Python script, and set the correct command line parameters (including Python script path and parameter 5)
Python enables automatic login/logout of Campus network Gateway