forked from Uepsilon/smartcard-webconnector-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webconnector.py
90 lines (70 loc) · 2.46 KB
/
webconnector.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
#! /usr/bin/env python
"""
Smartcard Webconnector - Idenify with your Smartcard!
Usage:
webconnector.py -U=<url> -E=<event-key> [-d=<delay>]
webconnector.py -h
webconnector.py --version
Options:
-U --url=<url> Url to POST to
-E --event-key=<event-key> Event-Key
-d --delay=<delay> Delay between Connection-Checks
-h --help Shows this Screen
--version Shows the Version
"""
import threading
import feedbackHandler
import time
import urllib2
from cardWebconnector import cardWebconnector
from smartcard.CardMonitoring import CardMonitor, CardObserver
from docopt import docopt
class Webconnector():
cardmonitor = None
cardobserver = None
"""
MainClass for the Webconnector
"""
def __init__(self, args):
feedbackHandler.setup()
self.cardmonitor = CardMonitor()
self.cardobserver = cardWebconnector(args)
self.addCardObserver(self.cardobserver)
def shutdown(self):
print "Shutdown"
self.removeCardObserver(self.cardobserver)
feedbackHandler.shutdown()
def addCardObserver(self, observer):
self.cardmonitor.addObserver(observer)
def removeCardObserver(self, observer):
self.cardmonitor.deleteObserver(observer)
def checkConnection(self, url):
try:
# Try connection
response = urllib2.urlopen(url, timeout=1)
# Connection successful, activate Connection-LED
feedbackHandler.setFeedback(feedbackHandler.CONNECTION, feedbackHandler.ACTIVE)
return True
except urllib2.URLError as e:
# No Connection to Server
feedbackHandler.setFeedback(feedbackHandler.CONNECTION, feedbackHandler.INACTIVE)
finally:
return False
if __name__ == '__main__':
try:
args = docopt(__doc__, version='Smartcard Webconnector: 0.1')
if args['--delay'] is None:
timeout = 10
else:
timeout = int(args['--delay'])
webconnector = Webconnector(args)
while True:
# Wait for SIGTERM and check connection every now and then
webconnector.checkConnection(args['--url'])
time.sleep(timeout)
finally:
try:
webconnector.shutdown()
except NameError:
# webconnector not defined, nothing to do here
pass