0%

桂林电子科技大学ip出校控制器python登陆

2017/10/3卸载了从14年开始使用的py3.4,同时也卸载了pywin插件。

说明

本文仅供学习,读者需要对自己行为负责,同时产生的行为与本文无关。读者视作默认同意本说明

程序在17年6月开发(ip出校控制器 2.1.4.0版本),后来只使用过一两次,就没使用过了,也不打算使用和维护了

开发环境

  1. python (本文使用python3.4)
  2. pywin32 (本文使用pywin32 for py3.4)
  3. ip出校控制器 2.1.4.0版本
    对于pywin32的安装,可以点我,下载相应的pywin32版本,然后点击下一步,下一步安装即可。

    windows句柄

    适当了解windows句柄有助于程序的移植,点我查看,wikipedia上对windows句柄的介绍

程序中句柄信息,直接通过spy++查找

源码

程序思路很简单,就是用py模拟人工输入信息,然后点击按钮。直接上代码;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# -*- coding: utf-8 -*-
"""
Created on Thur June 29 16:00:10 2017

@author: x
"""
import win32gui,win32api,win32con

def find_idxSubHandle(pHandle, winClass, index=0):
"""
已知子窗口的窗体类名
寻找第index号个同类型的兄弟窗口
pHandle父类句柄
winclass同类型句柄,index是同类型该句柄的索引
"""
assert type(index) == int and index >= 0
handle = win32gui.FindWindowEx(pHandle, 0, winClass, None)
while index > 0:
handle = win32gui.FindWindowEx(pHandle, handle, winClass, None)
index -= 1
return handle

def find_subHandle(pHandle, winClassList):
"""
递归寻找子窗口的句柄
pHandle是祖父窗口的句柄
winClassList是各个子窗口的class列表,父辈的list-index小于子辈
"""
assert type(winClassList) == list
if len(winClassList) == 1:
return find_idxSubHandle(pHandle, winClassList[0][0], winClassList[0][1])
else:
pHandle = find_idxSubHandle(pHandle, winClassList[0][0], winClassList[0][1])
return find_subHandle(pHandle, winClassList[1:])

def guet_connect(pHandle,idHandle,id,pwHandle,pw,Button_Handle):
"""
pHandle为父窗体句柄
idHandle为客户端用户名句柄
pwHandle为客户端密码句柄
Button_Handle为客户端连接按钮句柄
"""
ok_id=win32gui.SendMessage(idHandle, win32con.WM_SETTEXT, None, id)
ok_pw=win32gui.SendMessage(pwHandle, win32con.WM_SETTEXT, None, pw)
ok_button=win32api.SendMessage(pHandle, win32con.WM_COMMAND, 1, Button_Handle)
# 处理之后返回0
if ok_id==0 and ok_pw==0 and ok_button==0:
print("一次登陆尝试")

def check_connect(pHandle,Button_Handle):
"""
pHandle为父窗体句柄
Button_Handle为客户端查询按钮句柄
"""
chaxun_button=win32api.SendMessage(pHandle, win32con.WM_COMMAND, 1, Button_Handle)
if chaxun_button==0:
ie_error_Handle=win32gui.FindWindow(None,'http://172.16.1.1/ipmanager/login.jsp?id=0 - Internet Explorer')#登陆失败会获取该句柄
ie_ok_Handle=win32gui.FindWindow(None,'网络管理系统 - Internet Explorer')#登陆成功会获取该句柄
if ie_ok_Handle==0:
# 登陆成功
print("success")
close_ie=win32gui.PostMessage(ie_ok_Handle, win32con.WM_CLOSE, 0, 0)
return 1
elif ie_error_Handle==0:
#登陆失败
print("error")
close_ie=win32gui.PostMessage(ie_error_Handle, win32con.WM_CLOSE, 0, 0)
return 0

def write_file(student_id,student_pw):
success_passwd=open('ok_passwd.txt','a')
success_passwd.write('学号:%s\t密码:%s\n'%(student_id,student_pw))
print("成功记录一条数据:"+'学号:%s\t密码:%s\n'%(student_id,student_pw))

success_passwd.close

def get_message(hwnd):
buf_size = win32gui.SendMessage(hwnd, win32con.WM_GETTEXTLENGTH) + 1 # 要加上截尾的字节
str_buffer = win32gui.PyMakeBuffer(buf_size) # 生成buffer对象
win32api.SendMessage(hwnd, win32con.WM_GETTEXT, buf_size, str_buffer) # 获取buffer
# str_buffer = str(str_buffer[:-1]) # 转为字符串
address, length = win32gui.PyGetBufferAddressAndLen(str_buffer)
text = win32gui.PyGetString(address, length)
return text


class ipGUET(object):
def __init__(self, fgFilePath=None):
self.Mhandle = win32gui.FindWindow(None,'IP出校控制器')
# print ("IP 出校器初始化完成,父类句柄为%x"%(self.Mhandle) )
# TButton0_handle = find_subHandle(self.Mhandle, [("TButton",0)])
# TButton1_handle = find_subHandle(self.Mhandle,[("TComboBox",0),("Edit",0)])
self.TEdit_id_handle = find_subHandle(self.Mhandle, [("TEdit",1)])#用户名
self.TEdit_pw_handle = find_subHandle(self.Mhandle, [("TEdit",0)])#密码
self.TStatusBar_handle = find_subHandle(self.Mhandle, [("TStatusBar",0)])#版本信息
self.TButton_lianjie_handle=win32gui.FindWindowEx(self.Mhandle,0,None,"连接")
self.TButton_chaxun_handle=win32gui.FindWindowEx(self.Mhandle,0,None,"查询")
self.GroupBox_handle= find_subHandle(self.Mhandle, [("TGroupBox",0)])#余额

def start(self,student_id,student_pw):
guet_connect(self.Mhandle,self.TEdit_id_handle,student_id,self.TEdit_pw_handle,student_pw,self.TButton_lianjie_handle)
#print(get_message(TEdit_id_handle))
check_result=check_connect(self.Mhandle,self.TButton_chaxun_handle)
if check_result==111:
write_file(student_id,student_pw)


if __name__=="__main__":
# win32api.MessageBox(win32con.NULL, 'Python 你好!', '你好', win32con.MB_OK)
my_ipguet=ipGUET()
my_ipguet.start('你的学号','你的密码')

最后

注释已经写好了,程序只供学习

坚持原创技术分享,您的支持将鼓励我继续创作!