Skip to content

Instantly share code, notes, and snippets.

@bigeagle
Created August 13, 2012 08:42
Show Gist options
  • Save bigeagle/3338320 to your computer and use it in GitHub Desktop.
Save bigeagle/3338320 to your computer and use it in GitHub Desktop.
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