agregado autenticacion con contraseña del equipo (ZK800)
This commit is contained in:
parent
9cdf123a63
commit
e59bd5ead9
44
zk/base.py
44
zk/base.py
@ -8,6 +8,40 @@ from zk.attendance import Attendance
|
|||||||
from zk.exception import ZKErrorResponse, ZKNetworkError
|
from zk.exception import ZKErrorResponse, ZKNetworkError
|
||||||
from zk.user import User
|
from zk.user import User
|
||||||
|
|
||||||
|
def make_commkey(key, session_id, ticks=50):
|
||||||
|
"""take a password and session_id and scramble them to send to the time
|
||||||
|
clock.
|
||||||
|
copied from commpro.c - MakeKey"""
|
||||||
|
key = int(key)
|
||||||
|
session_id = int(session_id)
|
||||||
|
k = 0
|
||||||
|
for i in range(32):
|
||||||
|
if (key & (1 << i)):
|
||||||
|
k = (k << 1 | 1)
|
||||||
|
else:
|
||||||
|
k = k << 1
|
||||||
|
k += session_id
|
||||||
|
|
||||||
|
k = pack(b'I', k)
|
||||||
|
k = unpack(b'BBBB', k)
|
||||||
|
k = pack(
|
||||||
|
b'BBBB',
|
||||||
|
k[0] ^ ord('Z'),
|
||||||
|
k[1] ^ ord('K'),
|
||||||
|
k[2] ^ ord('S'),
|
||||||
|
k[3] ^ ord('O'))
|
||||||
|
k = unpack(b'HH', k)
|
||||||
|
k = pack(b'HH', k[1], k[0])
|
||||||
|
|
||||||
|
B = 0xff & ticks
|
||||||
|
k = unpack(b'BBBB', k)
|
||||||
|
k = pack(
|
||||||
|
b'BBBB',
|
||||||
|
k[0] ^ B,
|
||||||
|
k[1] ^ B,
|
||||||
|
B,
|
||||||
|
k[3] ^ B)
|
||||||
|
return k
|
||||||
|
|
||||||
class ZK(object):
|
class ZK(object):
|
||||||
|
|
||||||
@ -17,10 +51,11 @@ class ZK(object):
|
|||||||
__sesion_id = 0
|
__sesion_id = 0
|
||||||
__reply_id = 0
|
__reply_id = 0
|
||||||
|
|
||||||
def __init__(self, ip, port=4370, timeout=60):
|
def __init__(self, ip, port=4370, timeout=60, password=0):
|
||||||
self.__address = (ip, port)
|
self.__address = (ip, port)
|
||||||
self.__sock = socket(AF_INET, SOCK_DGRAM)
|
self.__sock = socket(AF_INET, SOCK_DGRAM)
|
||||||
self.__sock.settimeout(timeout)
|
self.__sock.settimeout(timeout)
|
||||||
|
self.__password = password # passint
|
||||||
|
|
||||||
def __create_header(self, command, command_string, checksum, session_id, reply_id):
|
def __create_header(self, command, command_string, checksum, session_id, reply_id):
|
||||||
'''
|
'''
|
||||||
@ -146,12 +181,17 @@ class ZK(object):
|
|||||||
response_size = 8
|
response_size = 8
|
||||||
|
|
||||||
cmd_response = self.__send_command(command, command_string, checksum, session_id, reply_id, response_size)
|
cmd_response = self.__send_command(command, command_string, checksum, session_id, reply_id, response_size)
|
||||||
|
self.__sesion_id = unpack('HHHH', self.__data_recv[:8])[2]
|
||||||
|
if cmd_response.get('code')==const.CMD_ACK_UNAUTH:
|
||||||
|
print "try auth"
|
||||||
|
command_string = make_commkey(self.__password,self.__sesion_id)
|
||||||
|
cmd_response = self.__send_command(const.CMD_AUTH, command_string , checksum, self.__sesion_id, self.__reply_id, response_size)
|
||||||
if cmd_response.get('status'):
|
if cmd_response.get('status'):
|
||||||
self.is_connect = True
|
self.is_connect = True
|
||||||
# set the session id
|
# set the session id
|
||||||
self.__sesion_id = unpack('HHHH', self.__data_recv[:8])[2]
|
|
||||||
return self
|
return self
|
||||||
else:
|
else:
|
||||||
|
print "connect err {} ".format(cmd_response["code"])
|
||||||
raise ZKErrorResponse("Invalid response")
|
raise ZKErrorResponse("Invalid response")
|
||||||
|
|
||||||
def disconnect(self):
|
def disconnect(self):
|
||||||
|
Loading…
Reference in New Issue
Block a user