#!/usr/bin/env
python
# -*- coding: gbk -*-
import urllib
import urllib2
import ClientCookie
import re
import string
import time
import socket
timeout=10
server="www.jb200.com"
loginurl="http://"+server+"/login.php"
v_account=[
{"name":"user1","pass":"pass1","utype":"1","vkey":"用户1"},
{"name":"user2","pass":"pass2","utype":"2","vkey":"用户2"}
]
def fes_write(s):
'''
控制输出信息的显示
'''
print s
def get_response(x):
'''
登录,取得登陆后的页面html代码,用来判断结果
'''
fes_write("login for "+ x["name"])
eform = {'username':x["name"], 'password':x["pass"], 'usertype':x["utype"]}
qstring = urllib.urlencode( eform )
#print qstring
try:
request = urllib2.Request( loginurl , qstring )
response = ClientCookie.urlopen( request )
return string.split( response.read(),"n")
except Exception, e:
fes_write(e)
return "error"
def verify_info(k,l):
'''
校验信息是否正确
'''
for line in l:
if k.match(line):
return True
break
return False
def verify_all():
'''
主程序,进行校验
'''
if timeout!=0:
socket.setdefaulttimeout(timeout )
fes_write("socket time out: " + str(socket.getdefaulttimeout()))
success_num=len(v_account)
for x in v_account:
vkey=re.compile('.*'+x["vkey"]+'.*')
if verify_info(vkey,get_response(x))==True:
success_num=success_num-1
fes_write(x["name"]+ " login success!")
else:
fes_write("error when " + x["name"]+ "login!")
if success_num==0:
fes_write("all things runs well!")
else:
e_notify()
fes_write("some thing is wrong!")
def e_notify():
'''
出错之后的notify操作
'''
pass
if __name__ == '__main__':
verify_all()