update some function
This commit is contained in:
parent
ded0d7bfd7
commit
98ba866d1a
16
test.py
16
test.py
@ -8,14 +8,14 @@ zk = zk.ZK('192.168.1.201')
|
||||
status, message = zk.connect()
|
||||
if status:
|
||||
print 'Firmware Version: : {}'.format(zk.get_firmware_version())
|
||||
users = zk.get_users()
|
||||
if users:
|
||||
for uid in users:
|
||||
if users[uid][2] == 14:
|
||||
level = 'Admin'
|
||||
else:
|
||||
level = 'User'
|
||||
print "[UID %d]: ID: %s, Name: %s, Level: %s, Password: %s" % ( uid, users[uid][0], users[uid][1], level, users[uid][3] )
|
||||
# users = zk.get_users()
|
||||
# if users:
|
||||
# for uid in users:
|
||||
# if users[uid][2] == 14:
|
||||
# level = 'Admin'
|
||||
# else:
|
||||
# level = 'User'
|
||||
# print "[UID %d]: ID: %s, Name: %s, Level: %s, Password: %s" % ( uid, users[uid][0], users[uid][1], level, users[uid][3] )
|
||||
|
||||
print zk.disconnect()
|
||||
else:
|
||||
|
139
zk/base.py
139
zk/base.py
@ -55,7 +55,9 @@ class ZK(object):
|
||||
|
||||
def __sending_packet(self, buf):
|
||||
self.__sock.sendto(buf, self.__address)
|
||||
self.__data_recv, addr = self.__sock.recvfrom(1024)
|
||||
|
||||
def __receive_packet(self, buf_size):
|
||||
self.__data_recv = self.__sock.recv(buf_size)
|
||||
|
||||
@property
|
||||
def __response(self):
|
||||
@ -81,6 +83,7 @@ class ZK(object):
|
||||
try:
|
||||
buf = self.__create_header(command=command)
|
||||
self.__sending_packet(buf)
|
||||
self.__receive_packet(8)
|
||||
|
||||
if self.__response == const.CMD_ACK_OK:
|
||||
return (True, self.__response)
|
||||
@ -98,6 +101,7 @@ class ZK(object):
|
||||
try:
|
||||
buf = self.__create_header(command=command, session_id=self.__sesion_id, reply_id=self.__reply_id)
|
||||
self.__sending_packet(buf)
|
||||
self.__receive_packet(8)
|
||||
if self.__response == const.CMD_ACK_OK:
|
||||
return (True, self.__response)
|
||||
else:
|
||||
@ -110,8 +114,10 @@ class ZK(object):
|
||||
try:
|
||||
buf = self.__create_header(command=command, session_id=self.__sesion_id, reply_id=self.__reply_id)
|
||||
self.__sending_packet(buf)
|
||||
self.__receive_packet(1024)
|
||||
if self.__response == const.CMD_ACK_OK:
|
||||
return self.__data_recv[8:]
|
||||
version = self.__data_recv[8:]
|
||||
return version
|
||||
else:
|
||||
return (False, self.__response)
|
||||
except Exception, e:
|
||||
@ -126,6 +132,7 @@ class ZK(object):
|
||||
try:
|
||||
buf = self.__create_header(command=command, session_id=self.__sesion_id, reply_id=self.__reply_id)
|
||||
self.__sending_packet(buf)
|
||||
self.__receive_packet(8)
|
||||
if self.__response == const.CMD_ACK_OK:
|
||||
return (True, self.__response)
|
||||
else:
|
||||
@ -133,69 +140,6 @@ class ZK(object):
|
||||
except Exception, e:
|
||||
return (False, e)
|
||||
|
||||
def __get_size_user(self):
|
||||
"""Checks a returned packet to see if it returned CMD_PREPARE_DATA,
|
||||
indicating that data packets are to be sent
|
||||
|
||||
Returns the amount of bytes that are going to be sent"""
|
||||
response = self.__response
|
||||
if response == const.CMD_PREPARE_DATA:
|
||||
size = unpack('I', self.__data_recv[8:12])[0]
|
||||
return size
|
||||
else:
|
||||
return 0
|
||||
|
||||
def get_users(self):
|
||||
"""Start a connection with the time clock"""
|
||||
command = const.CMD_USERTEMP_RRQ
|
||||
command_string = '\x05'
|
||||
chksum = 0
|
||||
|
||||
buf = self.__create_header(command=command, chksum=chksum, session_id=self.__sesion_id, reply_id=self.__reply_id, command_string=command_string)
|
||||
self.__sending_packet(buf)
|
||||
try:
|
||||
bytes = self.__get_size_user()
|
||||
userdata = []
|
||||
while bytes > 0:
|
||||
data_recv, addr = self.__sock.recvfrom(1032)
|
||||
userdata.append(data_recv)
|
||||
bytes -= 1024
|
||||
|
||||
self.__sock.recvfrom(8)
|
||||
|
||||
users = {}
|
||||
if len(userdata) > 0:
|
||||
# The first 4 bytes don't seem to be related to the user
|
||||
for x in xrange(len(userdata)):
|
||||
if x > 0:
|
||||
userdata[x] = userdata[x][8:]
|
||||
|
||||
userdata = ''.join(userdata)
|
||||
userdata = userdata[11:]
|
||||
|
||||
while len(userdata) > 72:
|
||||
uid, role, password, name, userid = unpack( '2s2s8s28sx31s', userdata.ljust(72)[:72])
|
||||
|
||||
uid = int( uid.encode("hex"), 16)
|
||||
# Clean up some messy characters from the user name
|
||||
password = password.split('\x00', 1)[0]
|
||||
password = unicode(password.strip('\x00|\x01\x10x'), errors='ignore')
|
||||
|
||||
userid = unicode(userid.strip('\x00|\x01\x10x'), errors='ignore')
|
||||
|
||||
name = name.split('\x00', 1)[0]
|
||||
|
||||
if not name:
|
||||
name = uid
|
||||
|
||||
users[uid] = (userid, name, int( role.encode("hex"), 16 ), password)
|
||||
|
||||
userdata = userdata[72:]
|
||||
|
||||
return users
|
||||
except Exception, e:
|
||||
return (False, e)
|
||||
|
||||
def power_off(self):
|
||||
'''
|
||||
shutdown device
|
||||
@ -205,9 +149,74 @@ class ZK(object):
|
||||
try:
|
||||
buf = self.__create_header(command=command, session_id=self.__sesion_id, reply_id=self.__reply_id)
|
||||
self.__sending_packet(buf)
|
||||
self.__receive_packet(8)
|
||||
if self.__response == const.CMD_ACK_OK:
|
||||
return (True, self.__response)
|
||||
else:
|
||||
return (False, self.__response)
|
||||
except Exception, e:
|
||||
return (False, e)
|
||||
|
||||
# def __get_size_user(self):
|
||||
# """Checks a returned packet to see if it returned CMD_PREPARE_DATA,
|
||||
# indicating that data packets are to be sent
|
||||
|
||||
# Returns the amount of bytes that are going to be sent"""
|
||||
# response = self.__response
|
||||
# if response == const.CMD_PREPARE_DATA:
|
||||
# size = unpack('I', self.__data_recv[8:12])[0]
|
||||
# return size
|
||||
# else:
|
||||
# return 0
|
||||
|
||||
# def get_users(self):
|
||||
# """Start a connection with the time clock"""
|
||||
# command = const.CMD_USERTEMP_RRQ
|
||||
# command_string = '\x05'
|
||||
# chksum = 0
|
||||
|
||||
# buf = self.__create_header(command=command, chksum=chksum, session_id=self.__sesion_id, reply_id=self.__reply_id, command_string=command_string)
|
||||
# self.__sending_packet(buf)
|
||||
# try:
|
||||
# bytes = self.__get_size_user()
|
||||
# userdata = []
|
||||
# while bytes > 0:
|
||||
# data_recv, addr = self.__sock.recvfrom(1032)
|
||||
# userdata.append(data_recv)
|
||||
# bytes -= 1024
|
||||
|
||||
# self.__sock.recvfrom(8)
|
||||
|
||||
# users = {}
|
||||
# if len(userdata) > 0:
|
||||
# # The first 4 bytes don't seem to be related to the user
|
||||
# for x in xrange(len(userdata)):
|
||||
# if x > 0:
|
||||
# userdata[x] = userdata[x][8:]
|
||||
|
||||
# userdata = ''.join(userdata)
|
||||
# userdata = userdata[11:]
|
||||
|
||||
# while len(userdata) > 72:
|
||||
# uid, role, password, name, userid = unpack( '2s2s8s28sx31s', userdata.ljust(72)[:72])
|
||||
|
||||
# uid = int( uid.encode("hex"), 16)
|
||||
# # Clean up some messy characters from the user name
|
||||
# password = password.split('\x00', 1)[0]
|
||||
# password = unicode(password.strip('\x00|\x01\x10x'), errors='ignore')
|
||||
|
||||
# userid = unicode(userid.strip('\x00|\x01\x10x'), errors='ignore')
|
||||
|
||||
# name = name.split('\x00', 1)[0]
|
||||
|
||||
# if not name:
|
||||
# name = uid
|
||||
|
||||
# users[uid] = (userid, name, int( role.encode("hex"), 16 ), password)
|
||||
|
||||
# userdata = userdata[72:]
|
||||
|
||||
# return users
|
||||
# except Exception, e:
|
||||
# return (False, e)
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user