让我帮写个小程序,查了查资料,用python写了一个,以前没用过,都是现查的资料,比较粗糙,放到这里留作备用。
1 import urllib.parse
2 import urllib.request
3
4 # ------------------------------需要修改1 begin---------------------------------
5 # 需要根据HttpFox插件修改 loginUrl,params.
6 loginUrl = ' http://localhost:4019/Login.aspx '
7 # "登录成功"/"登录失败" 应修改为手动操作web登录成功/失败时的提示信息
8 successMsg = ' 登录成功 '
9 failureMsg = ' 登录失败 '
10 # 把所有需要自动登录的用户和密码写到这个数组中
11 accounts = { ' 王大亮 ' : ' 1 ' , ' a1 ' : 123 , ' 二亮 ' : ' bbb ' }
12 # ------------------------------需要修改1 start---------------------------------
13
14 # ##定义一个自动登录函数
15 def AutoLogin(user,password):
16 try :
17 req = urllib.request.Request(loginUrl)
18 # ------------------------------需要修改2 begin-----------------------------------
19 req.add_header( ' Referer ' , ' http://localhost:4019/Login.aspx ' )
20 req.add_header( ' User-agent ' , ' Mozilla/5.0 ' )
21 req.add_header( ' Content-Type ' , ' application/x-www-form-urlencoded ' )
22 req.add_header( ' Accept-Charset ' , ' ISO-8859-1,utf-8;q=0.7,*;q=0.7 ' )
23 params = urllib.parse.urlencode({ ' txtUser ' : user,
24 ' txtPassword ' : password,
25 ' btnLogin ' : ' Button ' ,
26 ' __VIEWSTATE ' : ' /wEPDwULLTEwNjExNzk5MjgPZBYCAgMPZBYCAgEPDxYCHgRUZXh0BQznmbvlvZXlpLHotKVkZGR4O/u+ERWQGLzmZs3fMbjkx/qv6Q== ' ,
27 ' __EVENTVALIDATION ' : ' /wEWBALu+KqtCwLB2tiHDgK1qbSRCwKC3IeGDHKaazBbWjuZXNCLn8AMRkGzG+6O ' })
28 params = params.encode( ' ISO-8859-1 ' )
29 res = urllib.request.urlopen(req, params)
30 html = res.read().decode( ' utf-8 ' )
31 # 调试开关 可以查看登录成功后html页面中的关键字
32 # print(html)
33 # ------------------------------需要修改2 end-------------------------------------
34
35 if html.find(failureMsg) != - 1 :
36 return ' false '
37 elif html.find(successMsg) != - 1 :
38 return ' true '
39 except EnvironmentError as err:
40 return ' false '
41 return ' false '
42
43 print ( ' 程序正在执行,如果一直没有响应请强制关闭...... ' )
44 print ()
45 # ## 遍历所有用户密码,调用自动登录函数
46 for obj in accounts:
47 if (AutoLogin(obj,accounts[obj]) == ' false ' ):
48 print ( ' %s: 登录-->失败. ' % obj)
49 else :
50 print ( ' %s: 登录-->成功. ' % obj)
51 print ()
52 q = input( " 执行完毕,输入任意字符并按回车键退出程序: " )
2 import urllib.request
3
4 # ------------------------------需要修改1 begin---------------------------------
5 # 需要根据HttpFox插件修改 loginUrl,params.
6 loginUrl = ' http://localhost:4019/Login.aspx '
7 # "登录成功"/"登录失败" 应修改为手动操作web登录成功/失败时的提示信息
8 successMsg = ' 登录成功 '
9 failureMsg = ' 登录失败 '
10 # 把所有需要自动登录的用户和密码写到这个数组中
11 accounts = { ' 王大亮 ' : ' 1 ' , ' a1 ' : 123 , ' 二亮 ' : ' bbb ' }
12 # ------------------------------需要修改1 start---------------------------------
13
14 # ##定义一个自动登录函数
15 def AutoLogin(user,password):
16 try :
17 req = urllib.request.Request(loginUrl)
18 # ------------------------------需要修改2 begin-----------------------------------
19 req.add_header( ' Referer ' , ' http://localhost:4019/Login.aspx ' )
20 req.add_header( ' User-agent ' , ' Mozilla/5.0 ' )
21 req.add_header( ' Content-Type ' , ' application/x-www-form-urlencoded ' )
22 req.add_header( ' Accept-Charset ' , ' ISO-8859-1,utf-8;q=0.7,*;q=0.7 ' )
23 params = urllib.parse.urlencode({ ' txtUser ' : user,
24 ' txtPassword ' : password,
25 ' btnLogin ' : ' Button ' ,
26 ' __VIEWSTATE ' : ' /wEPDwULLTEwNjExNzk5MjgPZBYCAgMPZBYCAgEPDxYCHgRUZXh0BQznmbvlvZXlpLHotKVkZGR4O/u+ERWQGLzmZs3fMbjkx/qv6Q== ' ,
27 ' __EVENTVALIDATION ' : ' /wEWBALu+KqtCwLB2tiHDgK1qbSRCwKC3IeGDHKaazBbWjuZXNCLn8AMRkGzG+6O ' })
28 params = params.encode( ' ISO-8859-1 ' )
29 res = urllib.request.urlopen(req, params)
30 html = res.read().decode( ' utf-8 ' )
31 # 调试开关 可以查看登录成功后html页面中的关键字
32 # print(html)
33 # ------------------------------需要修改2 end-------------------------------------
34
35 if html.find(failureMsg) != - 1 :
36 return ' false '
37 elif html.find(successMsg) != - 1 :
38 return ' true '
39 except EnvironmentError as err:
40 return ' false '
41 return ' false '
42
43 print ( ' 程序正在执行,如果一直没有响应请强制关闭...... ' )
44 print ()
45 # ## 遍历所有用户密码,调用自动登录函数
46 for obj in accounts:
47 if (AutoLogin(obj,accounts[obj]) == ' false ' ):
48 print ( ' %s: 登录-->失败. ' % obj)
49 else :
50 print ( ' %s: 登录-->成功. ' % obj)
51 print ()
52 q = input( " 执行完毕,输入任意字符并按回车键退出程序: " )