设为首页 收藏本站
查看: 4598|回复: 0

[经验分享] python连接中控考勤机分析数据

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2015-4-3 09:01:14 | 显示全部楼层 |阅读模式
用python连接中控考勤机。 下载并分析数据,把结果邮件给人事。

中控SDK包: x32地址 x64地址
SDK包建议用32位的,在win7 64位系统上用64位开发包不行,用32可以。

python还要pywin32 注意版本,我这用的 32位的python 2.7 然后下的这个pywin32

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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/usr/bin/env python
#_*_ coding:gbk _*_

import win32com.client
import time
import sys
import smtplib
from email.mime.multipart import MIMEMultipart  
from email.mime.text import MIMEText  
from email.mime.image import MIMEImage  

def write_file(filename, data):
    with open(filename, 'w') as f:
        f.write(data)
     
def send_mail(filename=[], picname=[], content_txt='', content_html=''):
    smtpserver = 'smtp.163.com'  
    username = 'test@163.com'  
    password = 'abc123'  
     
    msg = MIMEMultipart()  
    msg['Subject'] = 'Check_In'  
    msg['From'] = "test@163.com"
    msg['To'] = "rev@163.com"
     
# attchment
    if len(filename) > 0:
        for i in filename:
            att = MIMEText(open(i, 'rb').read(), 'base64', 'gf2312')  
            att["Content-Type"] = 'application/octet-stream'  
            att["Content-Disposition"] = 'attachment; filename="%s"' % i.split('\\')[-1]
            msg.attach(att)  

# attchment picture
    if len(picname) > 0 and content_html != '':
        for i in range(0,len(picname)):
            #content_html = '<b>Some <i>HTML</i> text</b> and an image.<br><img src="cid:image%s"><br>good!' % i  
            msg_content_html = MIMEText(content_html,'html','gb2312')  
            msg.attach(msg_content_html)  
            
            with open(picname, 'rb') as f:
                msgImage = MIMEImage(f.read())  
            
            msgImage.add_header('Content-ID', '<image%s>' % (i + 1))  
            msg.attach(msgImage)
     
# content text   
    if content_txt != '':
        msg_content_txt = MIMEText(content_txt,_subtype='plain',_charset='gb2312')
        msg.attach(msg_content_txt)

# content html  
    if content_html != '' and len(picname) == 0:
        msg_content_html = MIMEText(content_html,_subtype='html',_charset='gb2312')
        msg.attach(msg_content_html)
         
    smtp = smtplib.SMTP()  
    smtp.connect(smtpserver)  
    smtp.starttls()  
    smtp.login(username, password)  
    smtp.sendmail(msg['From'], msg['To'], msg.as_string())  
    smtp.quit()      

def col_name():
    all = ','
    for i in uid_name:
        all = all + get_id(i) + ','
    return all
         
def get_id(idNum):
    try:
        return uid[idNum].split(u'\x00')[0].encode('gbk')
    except:
        return str(idNum)

zk = win32com.client.Dispatch('zkemkeeper.ZKEM.1')
if not zk.Connect_Net('192.168.1.2', 4370):
    print "Connect Error"
    sys.exit(1)

zk.SetDeviceTime(1)     #使用PC时间同步到考勤机
if time.localtime()[2] != 1:
    zk.Disconnect()
    sys.exit(1)
     
zk.ReadAllUserID(1)
uid = {}
while 1:
    exists, idNum, username, other, privilege, enable = zk.GetAllUserInfo(1)
    if not exists:
        break
    else:
        if enable:
            uid[idNum] = username

checkin = {}
last_month = time.localtime()[1]-1 or 12
if last_month == 12:
    cur_year = time.localtime()[0]-1
else:
    cur_year = time.localtime()[0]

if zk.ReadGeneralLogData(1):  #read All checkin data
    while 1:
        exists, machNum, idNum, emachNum, verifyMode, outMode, year, month, day, hour, minute = zk.GetGeneralLogData(1) #2
        if not exists:
            break
        if cur_year == year and last_month == month:
            if day not in checkin:
                checkin[day] = {}
            if idNum in checkin[day]:
                checkin[day][idNum].append(hour * 60 + minute)
            else:
                checkin[day][idNum] = [hour * 60 + minute]

zk.Disconnect()

csv_name = r'D:\CheckIn\%s-%s.csv' % (cur_year, last_month)
uid_name = sorted(uid.keys())
report = col_name() + '\n'

for dayNum in range(1,35):
    if dayNum not in checkin:
        break
    report = report + '%s-%s-%s,' % (cur_year, last_month, dayNum)
    for col in uid_name:
        if col not in checkin[dayNum]:
            report = report + ','
            continue  
        if len(checkin[dayNum][col]) < 2:
            report = report + '0,'
        else:
            dayTime = max(checkin[dayNum][col]) - min(checkin[dayNum][col])
            if dayTime%60 >= 45:
                report = report + str(dayTime//60 + 1) + ','
            elif 15 < dayTime%60 < 45:
                report = report + str(dayTime//60 + 0.5) + ','
            else:
                report = report + str(dayTime//60) + ','

    report = report + '\n'

write_file(csv_name, report)   
send_mail(filename=[csv_name], content_txt='Check_In %s-%s' % (cur_year, last_month))



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-53417-1-1.html 上篇帖子: 实用python监控代码 下篇帖子: python网页数据抓取全纪录 Socket 服务器 python 考勤机
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表