-
Notifications
You must be signed in to change notification settings - Fork 0
/
web_server.py
executable file
·79 lines (61 loc) · 2.09 KB
/
web_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
#!/usr/bin/python
import json
import web
from upmp.UpmpHandler import UpmpHandler
from exception.NetException import *
from exception.UpmpException import *
urls = (
'/', 'index',
'/api/charge', 'charge',
'/api/notify', 'notify',
'/api/merchant', 'merchant'
)
data_path = "data/upmp-mer-files"
class index:
def GET(self):
return "Hello, world!"
class charge:
def POST(self):
try:
data = web.data()
json_data = json.loads(data) if data else {}
res = UpmpHandler.charge(json_data)
web.header('Content-Type', 'application/json')
raise web.OK(json.dumps(res))
except InvalidMerchantException:
raise web.BadRequest('Invalid merchant')
except ChargeFailException:
raise web.BadRequest('Charge fail')
except InvalidContentTypeException:
raise web.BadRequest('Invalid content type')
class notify:
def POST(self):
try:
data = web.data()
UpmpHandler.notify(data)
web.header('Content-Type', 'text/html')
raise web.OK('success')
except InvalidMerchantException:
raise web.BadRequest('Invalid merchant')
except InvalidNotifyException:
raise web.BadRequest('Notify fail')
except InvalidContentTypeException:
raise web.BadRequest('Invalid content type')
except Exception, e:
print e
raise web.OK('success')
class merchant:
def GET(self):
merchant_data = UpmpHandler.query_merchant_info(data_path)
web.header('Content-Type', 'application/json')
raise web.OK(json.dumps(merchant_data))
class MyWebApplication(web.application):
def run(self, port=8080, *middleware):
func = self.wsgifunc(*middleware)
return web.httpserver.runsimple(func, ('0.0.0.0', port))
if __name__ == "__main__":
def notfound():
return web.notfound("Sorry, the page you were looking for was not found.")
app = MyWebApplication(urls, globals())
app.notfound = notfound
app.run(port=8085)