-
Notifications
You must be signed in to change notification settings - Fork 0
/
nonsense_server.py
59 lines (51 loc) · 1.73 KB
/
nonsense_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
from tornado.wsgi import WSGIContainer
from tornado.httpserver import HTTPServer
from tornado.ioloop import IOLoop
from nongen import getPoem
from flask import Flask, request, jsonify
from functools import wraps
app = Flask(__name__)
template = """
<html>
<body><center>
<i><font face="helvetica,georgia,courier,arial" color="#666666">*****</font></i>
</center></body>
</html>
"""
# allow jsonP
# mostly via https://gist.github.com/1094140
def jsonp(f):
global app
@wraps(f)
def decorated_function(*args, **kwargs):
callback = request.args.get('callback', False)
if callback:
content = str(callback) + '(' + str(f(*args,**kwargs).data) + ')'
return app.response_class(content, mimetype='application/javascript')
else:
return f(*args, **kwargs)
return decorated_function
@app.route("/", methods=['GET'])
def withParams():
url = request.args.get('url', 'http://eliotswasteland.tripod.com/twl.html')
lines = request.args.get('lines', '5')
words = request.args.get('words', '7')
return getPoem(url, int(lines), int(words), True)
@app.route("/short")
def shortPoem():
return template.replace("*****", getPoem("http://eliotswasteland.tripod.com/twl.html", 1, 7, True))
@app.route("/default")
def defaultPoem():
return getPoem("http://eliotswasteland.tripod.com/twl.html", 5, 7, True)
@app.route("/json")
@jsonp
def getJSON():
url = request.args.get('url', 'http://eliotswasteland.tripod.com/twl.html')
lines = request.args.get('lines', '5')
words = request.args.get('words', '7')
return jsonify({'poem': getPoem(url, int(lines), int(words), True)})
if __name__ == "__main__":
# run as Tornado server
http_server = HTTPServer(WSGIContainer(app))
http_server.listen(5000)
IOLoop.instance().start()