|
发表于 2004-4-1 13:26:21
|
显示全部楼层
[code:1]
#!/usr/bin/python
name = "Net logger for ZJG Campus of ZJU"
version = "v0.4"
author = "By Emfox@FreeCity"
server = "61.175.164.14"
headers = {'Accept': 'text/html', 'User-Agent': 'Mozilla',
'Content-Type': 'application/x-www-form-urlencoded'}
import httplib, urllib, socket
err_no=-1 # 0:sucess 1:regular error 2:system error
status='' # describe excute status, or show error message
def help():
"show help message"
print """ Usage: python zjg_logger.py [-h] [[-q] -u cardname -p password] [-l]
or long options:
[--help][--logout][[--query] --username cardnumber --password password]
-h or --help
print this message
-u or --username
input the cardnumber
-p or --password
input the password
-q or --query
query money left
-l or --logout
log out
Note: 1. please install python first, version of mine is 2.3.3 :)
2. -u and -p must be given both or neither
3. -l does not require the -u and -p paraments
4. -q does need supply the -u and -p paraments
4. if no paraments are given, program will use the GUI directly
Samples: python zjg_logger.py -u 800311112222@zheda -p 123456
python zjg_logger.py -q -u 800311112222@zheda -p 123456
python zjg_logger.py -l"""
print name+" "+version+" "+author
def get_ip():
"try to get local ip address, which was determined by dhcp server"
try:
socket_ip = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socket_ip.connect((server,80))
ip_addr = socket_ip.getsockname()[0]
socket_ip.close()
except socket.error, (temp, status):
ip_addr = ''
err_no = 2
return ip_addr
def login(id,password):
"login using the specified id and password"
global err_no, status
ip = get_ip()
if err_no == 2: return 0
path = '/servlet/scpservlet.UserDataReqLoginServlet'
params = urllib.urlencode({'id': id, \
'password': password, \
'httpIp': ip, \
'ip': ip, \
'servicetype': '201+', \
'languagetype': 0, \
'operatingSystem': 1, \
'RadiusVersion': 1, \
'Domain': 'hz163', \
'Submit': 'Login'})
try:
conn = httplib.HTTPConnection(server)
conn.request("POST", path, params, headers)
login_response = conn.getresponse()
the_cookie = login_response.getheader("set-cookie")
login_response.read()
except socket.error, (temp, status):
err_no = 2
return 0
if login_response.getheader("Location").find('/servlet/show.ShowHomePageServlet') != -1:
err_no = 1
status="Logined already"
else:
headers.update({"Cookie":the_cookie})
conn.request("GET",'/redirect.jsp','',headers)
login_response = conn.getresponse()
login_result = login_response.read()
if login_result.find("/servlet/show.ShowHomePageServlet") != -1:
err_no = 0
elif login_result.find("/escpjsp/webattestationdefeat.jsp") != -1:
err_no = 1
status = "Wrong password!"
else:
err_no = 1
status = "Repeated login"
conn.close()
def logout():
"Logout (no id or password is needed)"
global err_no, status
ip = get_ip()
if err_no == 2: return 0
path = '/servlet/scpservlet.CloseServiceReqServlet'
params = urllib.urlencode({'ip': ip,
'ISNNO': '1006',
'id': '',
'languagetype': 1,
'httpIp': ip})
try:
conn = httplib.HTTPConnection(server)
conn.request("GET", path, params, headers)
logout_response = conn.getresponse()
the_cookie = logout_response.getheader("set-cookie")
logout_response.read()
except socket.error, (temp, status):
err_no = 2
return 0
headers.update({"Cookie":the_cookie})
conn.request("GET",'/redirect.jsp','',headers)
logout_response = conn.getresponse()
logout_result = logout_response.read()
if logout_result.find("/show/m00msg.jsp") != -1:
err_no = 0
else:
err_no = 1
status = "Repeated logout"
conn.close()
def query(id, password):
"query money left"
global err_no, status
ip = get_ip()
if err_no == 2: return 0
path = '/servlet/scpservlet.UserDataReqLoginServlet'
params = urllib.urlencode({'id': id, \
'password': password, \
'httpIp': ip, \
'ip': ip, \
'servicetype': 'portallocal', \
'languagetype': 0, \
'operatingSystem': 1, \
'RadiusVersion': 1, \
'Domain': 'hz163', \
'Submit': 'Login'})
try:
conn = httplib.HTTPConnection(server)
conn.request("POST", path, params, headers)
login_response = conn.getresponse()
the_cookie = login_response.getheader("set-cookie")
login_response.read()
except socket.error, (temp, status):
err_no = 2
return 0
headers.update({"Cookie":the_cookie})
conn.request("GET",'/redirect.jsp','',headers)
login_response = conn.getresponse()
login_result = login_response.read()
if login_result.find("/servlet/show.ShowHomePageServlet") != -1:
err_no = 0
path='/servlet/smpservlet.QueryLeftServlet'
conn.request("GET", path, '', headers)
query_response = conn.getresponse()
query_result = query_response.read()
query_position = query_result.find("Account Left:")+96
money_left = query_result[query_position:query_position+5]
elif login_result.find("/kfautherror.jsp") != -1:
err_no = 1
status = "Wrong password!"
money_left=''
else:
err_no = 1
status = "Repeated query"
money_left=''
conn.close()
return money_left
import sys, getopt
id_flag = 0
password_flag = 0
query_flag = 0
try:
opts, args = getopt.gnu_getopt(sys.argv[1:],"hu:p:ql", \
["help", "username=", "password=", "query", "logout"])
except getopt.GetoptError:
help()
sys.exit()
for o, a in opts:
if o in ("-h", "--help"):
help()
sys.exit()
if o in ("-u", "--username"):
uname = a
id_flag = 1
if o in ("-p", "--password"):
password = a
password_flag = 1
if o in ("-l", "--logout"):
logout()
if err_no == 0:
print "Logout success"
else:
print "Error: "+status
sys.exit()
if o in ("-q", "--query"):
query_flag = 1
if id_flag * password_flag == 1:
if query_flag == 1:
money_left = query(username,password)
if err_no == 0:
print "Money left: "+money_left
else:
print "Error: "+status
sys.exit()
else:
login(username,password)
if err_no == 0:
print "Login success: "+username
else:
print "Error: "+status
sys.exit()
from Tkinter import *
class Application(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.grid()
self.createWidgets()
def login(self):
login(self.username.get()+self.logintype.get(), \
self.password.get())
if err_no == 0:
self.message.set("Login success: "+ \
self.username.get()+self.logintype.get())
else:
self.message.set("Error: "+status)
def logout(self):
logout()
if err_no == 0:
self.message.set("Logout success")
else:
self.message.set("Error: "+status)
def query(self):
money_left = query(self.username.get(), \
self.password.get())
if err_no ==0:
self.message.set("Money left: "+money_left)
else:
self.message.set("Error: "+status)
def createWidgets(self):
self.username = StringVar(master=None)
self.password = StringVar(master=None)
self.logintype = StringVar(master=None)
self.logintype.set("@zheda")
self.message = StringVar(master=None)
self.message.set(author)
self.LabelU = Label(self, text="Card Number:")
self.LabelP = Label(self, text="Password:")
self.LabelU.grid(column=0,row=0)
self.LabelP.grid(column=0,row=1)
self.EntryU = Entry(self, width=20, \
textvariable=self.username)
self.EntryU.grid(column=1,row=0,columnspan=2)
self.EntryP = Entry(self, show="*", \
textvariable=self.password)
self.EntryP.grid(column=1,row=1,columnspan=2)
self.Radio1 = Radiobutton(self, text="@zheda", \
value="@zheda", \
variable=self.logintype)
self.Radio1.grid(column=3,row=0)
self.Radio2 = Radiobutton(self, text="Internet", \
value='', \
variable=self.logintype)
self.Radio2.grid(column=3,row=1)
self.ButtonLogin = Button (self, text="Login ", \
command=self.login)
self.ButtonLogin.grid(column=4,row=0)
self.ButtonLogout = Button (self, text="Logout", \
command=self.logout)
self.ButtonLogout.grid(column=4,row=1)
self.ButtonQuery = Button (self, text="Query ", \
command=self.query)
self.ButtonQuery.grid(column=4,row=2)
self.LabelMessage = Label(self, relief='groove', \
anchor='w', \
textvariable=self.message)
self.LabelMessage.grid(column=0,row=2,columnspan=4,sticky='we')
logger = Application()
logger.master.title(name+" "+version)
logger.master.resizable(width=0, height=0)
logger.mainloop()
[/code:1] |
|