Skip to content

Instantly share code, notes, and snippets.

@bigeagle bigeagle/thunet.py
Created Aug 13, 2012

Embed
What would you like to do?
THU net connector via web
#!/usr/bin/env python2
# -*- coding:utf8 -*-
import requests
import argparse
USERNAME=""
PASSWORD=""
BASE_URL="http://net.tsinghua.edu.cn/"
COOKIE_URL="/index_wired.html"
FORM_URL="/cgi-bin/do_login"
LOGOUT_URL="/cgi-bin/do_logout"
def make_data_and_cookies(username, password):
r = requests.get(BASE_URL+COOKIE_URL)
data = {
"uname": username,
"pass": password,
"drop": 0,
"is_pad":2,
"n":100,
"type":1,
}
return data, r.cookies
def submit_form(data,cookies):
form_action_url = BASE_URL + FORM_URL
r = requests.post(form_action_url, data=data, cookies=cookies)
print r.text
def login(username, password):
data, cookies = make_data_and_cookies(username, password)
submit_form(data, cookies)
def logout():
r = requests.get( BASE_URL+LOGOUT_URL )
print r.text
if __name__ == '__main__':
parser = argparse.ArgumentParser(description="THUnet login via web.")
parser.add_argument('-k', '--kill', action="store_true", help="Disconnect/Log out.")
parser.add_argument('-u', '--username', default=USERNAME, help="Username")
parser.add_argument('-p', '--password', default=PASSWORD, help="Password")
args = parser.parse_args()
if args.kill:
logout()
else:
login(args.username, args.password)
# vim: ts=4 sw=4 sts=4 expandtab
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.