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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
|
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import argparse
import getpass
import json
import os
import sys
import cherrypy
__author__ = 'Simeon Simeonov'
__version__ = '1.0-beta'
__license__ = "GPL3"
class WebNotifyServer(object):
def __init__(self, config):
"""
"""
self.__config = config
self.__instances = dict()
self.__queues = dict()
try:
for instance in self.__config['server']['instances']:
self.__queues[instance['name']] = list()
self.__instances[instance['name']] = instance
except Exception as e:
sys.stderr.write('Unable to initialize queues: {0}'.format(e))
sys.exit(1)
@cherrypy.expose
def index(self):
"""
default dispatcher
"""
return 'index'
@cherrypy.expose
def default(self, *args):
"""
default dispatcher
"""
return 'default'
@cherrypy.expose
def push(self, *args, **kwargs):
if not args:
raise cherrypy.HTTPError(400)
print('args: {0}'.format(args))
print('kwargs: {0}'.format(kwargs))
instance = self.__get_instance(args[0])
if not kwargs.get('password') == instance['password']:
raise cherrypy.HTTPError('403 Forbidden',
'Wrong instance or password')
return 'OK'
def __get_instance(self, name):
"""
returns a matching 'instance' dictionary
cherrypy.HTTPError - "403 Forbidden" is raised if no
instance matches 'name'
"""
try:
for instance in self.__config['server']['instances']:
if name == instance['name']:
return instance
except Exception as e:
raise cherrypy.HTTPError('403 Forbidden',
'Wrong instance or password: {0}'.format(e))
raise cherrypy.HTTPError("403 Forbidden", "Wrong instance or password")
# mappings = [
# (r'^/push/', push),
# (r'^.*$', default),
# ]
def main():
parser = argparse.ArgumentParser(description='The following options are available')
parser.add_argument('-d',
action='store_true',
dest='daemonize',
default=False,
help='daemonize the server process')
parser.add_argument('-H', '--hostname',
metavar='HOSTNAME',
type=str,
dest='hostname',
default='127.0.0.1',
help="Server IP / hostname")
parser.add_argument('-p', '--port',
metavar='PORT',
type=int,
dest='port',
default=9998,
help="Server port")
parser.add_argument('-c', '--config-file',
metavar='FILE',
type=str,
default=os.path.expanduser('~/.beinc_server.json'),
dest='config_file',
help="config file")
parser.add_argument('-v', '--version',
action='version',
version='%(prog)s {0}'.format(__version__),
help='display program-version and exit')
args = parser.parse_args()
try:
with open(args.config_file, 'r') as fp:
config_dict = json.load(fp)
except Exception as e:
sys.stderr.write('Unable to parse {0}: {1}'.format(args.config_file, e))
cherrypy.config.update({
'server.socket_host': args.hostname,
'server.socket_port': args.port,
'server.ssl_module': config_dict['server']['general']['ssl_module'].encode('utf-8'),
'server.ssl_certificate': config_dict['server']['general']['ssl_certificate'],
'server.ssl_private_key': config_dict['server']['general']['ssl_private_key'],
})
try:
cherrypy.quickstart(WebNotifyServer(config_dict))
except Exception as e:
sys.stderr.write("WebServer error: {0}".format(e))
sys.exit(1)
sys.exit(0)
if __name__ == "__main__":
main()
|