3705

积分

好友

主题

[其他问题] UW 自动选课系统

发表于 2012-12-10 00:02:59 | 查看: 937| 回复: 2
这是一位前辈用python写的,我没有试过,
原文地址:https://github.com/elodin/auto-r ... ter/AutoRegister.py
By elodin
给大家参考下:
[code=python]import mechanize
import cookielib
import sys
import time
import datetime
import smtplib

CONST_PROGRAM_NAME = 'AutoRegisterUW'

def send_SMS_text(msg, recipient, gmail_user, gmail_pass):
    server = smtplib.SMTP("smtp.gmail.com", 587)
    server.starttls()
    server.login(gmail_user, gmail_pass)
    server.sendmail(CONST_PROGRAM_NAME, recipient, msg)
   
# Browser
br = mechanize.Browser()

# Cookie Jar
cj = cookielib.LWPCookieJar()
br.set_cookiejar(cj)

# Browser options
br.set_handle_equiv(True)
#br.set_handle_gzip(True)
br.set_handle_redirect(True)
br.set_handle_referer(True)
br.set_handle_robots(False)

# Follows refresh 0 but not hangs on refresh > 0
br.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=1)

#br.set_debug_http(True)
#br.set_debug_redirects(True)
#br.set_debug_responses(True)

#br.addheaders = [('User-agent', 'Mozilla/5.0 (X11; U; Linux i686; en-US; ' +
#                  'rv:1.9.0.1) Gecko/2008071615 Fedora/3.0.1-1.fc9 ' +
#                  'Firefox/3.0.1')]

print 'Input UW credentials.'
UW_user = raw_input("User: ")
UW_pass = raw_input("Pass: ")
sln = str(input("SLN : "))
print

wait_time_s = -1
while (wait_time_s < 30):
    try:
        wait_time_s = input('Input seconds to wait between queries (>= 30). ')
    except:
        continue
print

print 'Input Gmail credentials.'
gmail_user = raw_input("User: ")
gmail_pass = raw_input("Pass: ")
print

print 'Input [email protected][SMS gateway].'
address = raw_input("Gateway: ")
print

try:
    print 'Testing text service...'
    send_SMS_text('Welcome to ' + CONST_PROGRAM_NAME, address, gmail_user, gmail_pass)
    print 'Text sent.'
except:
    print 'Error sending text.'
print

newly_logged_in = False
class_open = False
while True:
    br.open('https://weblogin.washington.edu/')
   
    try:
        br.select_form(name='query')
    #    print br.form
        br.form.set_all_readonly(False)
        br['user'] = UW_user
        br['pass'] = UW_pass
        br.submit()
        html = br.response().read()
    #    print html
        if 'Login failed' in html:
            print 'UW login failed.'
            exit(0)
        else:
            newly_logged_in = True;
            print 'UW login successful.'
    except mechanize.FormNotFoundError:
        False
   
    br.open("https://sdb.admin.washington.edu/timeschd/uwnetid/sln.asp?" +
            "QTRYR=AUT+2012&" +
            "SLN=" + sln)
    if newly_logged_in:
        br.select_form(name='relay')
        br.submit()
        br.select_form(name='relay')
        br.submit()
        newly_logged_in = False
        
    html = br.response().read()
    prev = class_open
    class_open = 'Open' in html
    if class_open:
        print
        print "Open at", str(datetime.datetime.now())
    else:
        sys.stdout.write('.')
#        print "Closed at", str(now)
    if not class_open == prev:
        if class_open:
            msg = sln + ' is open.'
        else:
            msg = sln + ' is closed.'
        try:
            send_SMS_text(msg, address, gmail_user, gmail_pass)
            print 'Text sent.'
        except smtplib.SMTPAuthenticationError:
            print 'Gmail login failed.'
        except:
            print 'Error sending text.'
    time.sleep(wait_time_s)
        [/code]
发表于 2012-12-10 00:33:51
这..怎么跑啊

点评 回复 显示全部楼层 道具 举报

admin 发表于 2012-12-10 05:33
电脑安python后,去cmd里运行。python xxxxx.py
发表于 2013-6-16 04:32:24
好厉害

回复 显示全部楼层 道具 举报

您需要登录后才可以回帖 登录 | 立即注册

手机版|Copyright|CSUW

GMT-8, 2019-5-23 03:23 , Processed in 0.110454 second(s), 24 queries .

Powered by CSUW

© 2001-2012 CSUW.ORG

返回顶部