-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.py
92 lines (77 loc) · 2.48 KB
/
server.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
91
92
# -*- coding: utf-8 -*-
import json
import os
from copy import copy
from optparse import OptionParser
from gevent import monkey; monkey.patch_all()
from gevent import pywsgi
results = {}
template = {
'failure': 0,
'canonical_ids': 0,
'success': 0,
'multicast_id': 6616353152392206975,
'results': []
}
def response_ok():
response = copy(template)
response['failure'] = 0
response['success'] = 1
response['results'].append({u'message_id': u'0:1370674827295849'})
return response
def response_invalid():
response = copy(template)
response['failure'] = 1
response['success'] = 0
response['results'].append({u'error': u'InvalidRegistration'})
return response
def response_update():
response = copy(template)
response['failure'] = 0
response['success'] = 1
response['canonical_ids'] = 1
response['results'].append({u'message_id': u'0:1370674827295849', u'registration_id': 'tokenupdated'})
return response
ops = {
'OK': response_ok,
'INVALID': response_invalid,
'UPDATE': response_update
}
def handle(env, start_response):
body =json.loads(env['wsgi.input'].read())
regid = body['registration_ids'][0] # only one reg id is supported
if regid in results:
response = ops[results[regid].strip().upper()]()
else:
# by default response will be ok
response = response_ok()
start_response('200 OK', [('Content-Type', 'application/json')])
print response
return [json.dumps(response)]
def main():
parser = OptionParser()
parser.add_option(
"-p", "--port",
dest="port",
help="Server port [%default]",
default=8081)
parser.add_option(
"-b", "--bind_address",
dest="bind",
help="Bind addreess [%default]",
default="0.0.0.0")
parser.add_option(
"-r", "--results_file",
dest="rfile",
help="Results file [%default]",
default="results.csv")
(options, args) = parser.parse_args()
# By default server outputs ok responses, but a results file could be defined to define responses.
if os.path.exists(options.rfile):
print 'Loaded %s file' % options.rfile
global results
results = dict(x.strip().split(',') for x in open(options.rfile, 'r'))
print "Starting server on %s:%s" % (options.bind, options.port)
pywsgi.WSGIServer((options.bind, options.port), handle).serve_forever()
if __name__ == "__main__":
main()