-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathpywebapp.py
executable file
·57 lines (50 loc) · 1.58 KB
/
pywebapp.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
#!/usr/bin/env python
import json
import socket
import web
urls = ('/', 'Default',
'/api/(.*)', 'Api')
class Default:
def GET(self):
web.header('Content-Type', 'text/html')
hostname = socket.gethostname()
return """<html>
<title>PyWebApp</title>
<body bgcolor="white">
<h1>A00123456x, Bill Klug</h1>
<p>Available API methods:</p>
<ul>
<li><a href="/api/v1/test">/api/v1/test</a></li>
<li><a href="/api/v1/json">/api/v1/json</a></li>
<li><a href="/api/v1/worker">/api/v1/worker</a></li>
</ul>
<em>{}</em>
</body>
</html>""".format(hostname)
class Api:
def GET(self, name):
if name == 'v1/test':
return self.test()
elif name == 'v1/json':
return self.json()
elif name == 'v1/worker':
return self.worker()
else:
web.header('Content-Type', 'text/html')
web.ctx.status = '404 Not Found'
return "undefined"
def test(self):
web.header('Content-Type', 'application/json')
return json.dumps({'test': True})
def json(self):
web.header('Content-Type', 'application/json')
return json.dumps({'api-version': 'v1', 'app-name': 'pywebapp'})
def worker(self):
web.header('Content-Type', 'application/json')
hostname = socket.gethostname()
return json.dumps({'worker-name': hostname})
web.config.debug = False
app = web.application(urls, globals())
if __name__ == '__main__':
app.run()
# vim:ai ts=4 sw=4 et: