Created
May 9, 2013 04:08
-
-
Save zhangyoufu/5545492 to your computer and use it in GitHub Desktop.
计算机组成实验室签到(内网)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
stu_id = 'your student id' | |
username = password = 'use your own credential, hint: 00xxxxx' | |
host = '10.14.101.169' | |
login_url = 'http://10.14.101.169/LogOn' | |
lab_url ='http://10.14.101.169/TA/lab/Edit/114' | |
check_url = 'http://10.14.101.169/QianDao' | |
from urllib import urlencode | |
import re | |
import urllib2 | |
import cookielib | |
class NoRedirection(urllib2.HTTPErrorProcessor): | |
def http_response(self, request, response): | |
return response | |
cookie_jar = cookielib.CookieJar() | |
opener = urllib2.build_opener( urllib2.HTTPCookieProcessor(cookie_jar), NoRedirection ) | |
def login(): | |
print 'Login...', | |
login_form = urlencode(dict(UserName=username, Password=password)) | |
resp = opener.open(login_url, login_form) | |
assert resp.getcode() == 302 | |
print 'Success' | |
def ip_acl(form=None, ip=None): | |
if form is None: | |
print 'ip_acl Fetch...', | |
resp = opener.open(lab_url) | |
assert resp.getcode() == 200 | |
data = resp.read() | |
match_list = re.findall( '(?s)<(?:(input)|(textarea)|select)[^>]*?name="([^"]+)"(?(1)[^>]*?value="([^"]+)"|.*?>(?(2)\r\n(.*?)<|.*?selected.*?value="(.*?)"))', data ) | |
form = dict( [(match[2], match[3] or match[4] or match[5]) for match in match_list] ) | |
ip_list = form['AllowedIPs'] | |
print ip_list | |
if ip is None: | |
ip = re.search( u'您当前的IP地址是:(.*?)<'.encode('utf-8'), data ).group(1) | |
print 'Client IP:', ip | |
form['AllowedIPs'] += ', ' + ip | |
print 'Commit change...', | |
resp = opener.open(lab_url, urlencode(form)) | |
assert resp.getcode() == 302 | |
opener.open(lab_url) | |
print 'Success' | |
if ip is not None: | |
form['AllowedIPs'] = ip_list | |
return form | |
def check(): | |
resp = opener.open(check_url, urlencode( dict(stuno=stu_id) )) | |
assert resp.getcode() == 302 | |
resp = opener.open(check_url) | |
assert resp.getcode() == 200 | |
data = re.search( '(?s)content_inner_inner">(.*)<form', resp.read() ).group(1).strip() | |
for msg in [ s.decode('utf-8').strip() for s in re.findall( '(?s)>([^<]+?)<', data ) ]: | |
print msg | |
cookie_jar.clear(host, '/', 'LockOnQiandao') | |
def main(): | |
login() | |
backup = ip_acl() | |
check() | |
ip_acl(backup) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment