Skip to content

Commit f9e94e8

Browse files
author
Oliver Simon
committed
Initial import of the Python EKS library with example and test code.
1 parent 6be29ac commit f9e94e8

File tree

4 files changed

+257
-0
lines changed

4 files changed

+257
-0
lines changed

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Python EKS Library
2+
3+
## Usage
4+
5+
See example code in demo.py. Hostname needs to be set, port is 2444 by default.
6+
You need to implement a callback class to receive notitications from the eks library.

demo.py

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
from eks import EKSConnector, EKSCallback
2+
import datetime, time
3+
4+
class EKSConnectorMock(EKSConnector):
5+
6+
def __init__ (self, callback, host, port=2444):
7+
super(EKSConnectorMock, self).__init__(callback, host, port)
8+
self.expiration_date = datetime.datetime.now() + datetime.timedelta(seconds=5)
9+
self.is_active = False
10+
11+
def read_key_state(self, callback): # override networking
12+
now = datetime.datetime.now()
13+
14+
if self.is_active:
15+
callback.did_insert_key()
16+
callback.did_read_key("12345678")
17+
else:
18+
callback.did_remove_key()
19+
20+
if now > self.expiration_date:
21+
self.is_active = not self.is_active
22+
self.expiration_date = datetime.datetime.now() + datetime.timedelta(seconds=5)
23+
24+
class MyEKSCallback(EKSCallback):
25+
26+
def did_insert_key(self):
27+
print "did insert key"
28+
29+
def did_remove_key(self):
30+
print "did remove key"
31+
32+
def did_read_key(self, data):
33+
print "did read key: %s" % data
34+
35+
callback = MyEKSCallback()
36+
eks = EKSConnector("127.0.0.1", 2444)
37+
38+
print "single call: "
39+
eks.read_key_state(callback)
40+
41+
print "\npermanent call: "
42+
eks.start_listening(callback)

eks.py

+118
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
import socket, threading, time
2+
3+
4+
class EKSResponse:
5+
def __init__(self, command, status, payload):
6+
self.command = command
7+
self.status = status
8+
self.payload = payload
9+
10+
def __eq__(self, other):
11+
if other == None:
12+
return False
13+
return self.command == other.command \
14+
and self.status == other.status \
15+
and self.payload == other.payload
16+
17+
18+
class EKSConnector(object):
19+
20+
def __init__ (self, host, port=2444, timeout=5):
21+
self.host = host
22+
self.port = port
23+
self.poll_interval = 1
24+
self.socket_timeout = timeout
25+
26+
27+
def __poll(self):
28+
last_response = None
29+
while self.polling_enabled:
30+
self.read_key_state(self.callback)
31+
time.sleep(self.poll_interval)
32+
33+
34+
def __send_to_socket(self, msg):
35+
bytes_sent = self.eks_socket.send(msg)
36+
return bytes_sent
37+
38+
39+
def __read_from_socket(self):
40+
chunks = []
41+
bytes_recd = 0
42+
msglen = 123
43+
command = ''
44+
status = -1
45+
start = 0
46+
length = 0
47+
48+
while bytes_recd < msglen:
49+
chunk = self.eks_socket.recv(1)
50+
if chunk == '':
51+
raise RuntimeError("socket connection broken")
52+
chunks.append(chunk.encode("hex"))
53+
54+
if bytes_recd == 0:
55+
msglen = ord(chunk)
56+
if bytes_recd == 1:
57+
command += chunk
58+
if bytes_recd == 2:
59+
command += chunk
60+
if bytes_recd == 6:
61+
status = ord(chunk)
62+
63+
bytes_recd = bytes_recd + len(chunk)
64+
65+
payload = ''.join(chunks[8:len(chunks)])
66+
return EKSResponse(command, status, payload)
67+
68+
69+
def read_key_state(self, callback):
70+
self.eks_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
71+
self.eks_socket.settimeout(self.socket_timeout)
72+
self.eks_socket.connect((self.host, self.port))
73+
74+
response = self.__read_from_socket()
75+
self.__handle_response(response, callback)
76+
77+
if response.command == "Ek" and response.status == 1:
78+
self.__send_to_socket("\x07TL\x01\x01\x73\x09")
79+
response = self.__read_from_socket()
80+
self.__handle_response(response, callback)
81+
82+
self.eks_socket.close()
83+
return response
84+
85+
86+
def start_listening(self, callback, interval=1):
87+
self.polling_enabled = True
88+
self.callback = callback
89+
self.poll_interval = interval
90+
self.__poll()
91+
92+
93+
def stop_listening(self):
94+
self.polling_enabled = False
95+
96+
97+
def __handle_response(self, response, callback):
98+
if response.command == "Ek":
99+
if response.status == 1:
100+
callback.did_insert_key()
101+
elif response.status == 2:
102+
callback.did_remove_key()
103+
elif response.status == 3:
104+
raise RuntimeError
105+
elif response.command == "RL":
106+
callback.did_read_key(response.payload)
107+
108+
109+
class EKSCallback: #abstract
110+
111+
def did_insert_key(self):
112+
raise NotImplementedError
113+
114+
def did_remove_key(self):
115+
raise NotImplementedError
116+
117+
def did_read_key(self, data):
118+
raise NotImplementedError

test.py

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import socket
2+
import time
3+
4+
class Response:
5+
def __init__(self, status, payload):
6+
self.status = status
7+
self.payload = payload
8+
9+
class mysocket:
10+
'''demonstration class only
11+
- coded for clarity, not efficiency
12+
'''
13+
14+
def __init__(self, sock=None):
15+
if sock is None:
16+
self.sock = socket.socket(
17+
socket.AF_INET, socket.SOCK_STREAM)
18+
else:
19+
self.sock = sock
20+
21+
def connect(self, host, port):
22+
self.sock.connect((host, port))
23+
24+
def close(self):
25+
self.sock.close()
26+
27+
def mysend(self, msg):
28+
bytes_sent = self.sock.send(msg)
29+
return bytes_sent
30+
# totalsent = 0
31+
# while totalsent < len(msg):
32+
# sent = self.sock.send(msg[totalsent:])
33+
# if sent == 0:
34+
# raise RuntimeError("socket connection broken")
35+
# totalsent = totalsent + sent
36+
37+
def myreceive(self):
38+
chunks = []
39+
bytes_recd = 0
40+
msglen = 123
41+
command = ''
42+
status = -1
43+
44+
while bytes_recd < msglen:
45+
chunk = self.sock.recv(1)
46+
if chunk == '':
47+
raise RuntimeError("socket connection broken")
48+
chunks.append(chunk)
49+
50+
if bytes_recd == 0:
51+
msglen = ord(chunk)
52+
if bytes_recd == 1:
53+
command += chunk
54+
if bytes_recd == 2:
55+
command += chunk
56+
if bytes_recd == 6:
57+
status = ord(chunk)
58+
59+
bytes_recd = bytes_recd + len(chunk)
60+
# print "(" + str(bytes_recd) + ") \t" + str(ord(chunk)) + "\t" + str(msglen)
61+
62+
63+
# if bytes_recd > 3:
64+
# print command + "/" + str(status)
65+
# print ''.join(chunks)
66+
if command == "Ek":
67+
if status == 1:
68+
return Response("EKS_KEY_IN", None)
69+
if status == 2:
70+
return Response("EKS_KEY_OUT", None)
71+
if status == 3:
72+
return Response("EKS_KEY_OTHER", None)
73+
else:
74+
return Response(command, ''.join(chunks))
75+
76+
77+
mysock = mysocket()
78+
mysock.connect("127.0.0.1", 2444)
79+
status = ''
80+
while True:
81+
mysock.mysend("\x07Ek\x01\x00\x00\x00")
82+
new_status = mysock.myreceive()
83+
if new_status != status:
84+
print 'new status'
85+
status = new_status
86+
print new_status
87+
print status
88+
89+
if status == "EKS_KEY_IN":
90+
mysock.mysend("\x07TL\x01\x00\x00\x74")
91+
print mysock.myreceive()

0 commit comments

Comments
 (0)