Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
61 lines (49 sloc) 1.84 KB
import optparse
import simplejson
import bottle
import tornado.web
import tornado.ioloop
class ResponseGenerator(object):
def __init__(self, filepath):
self.entries = []
self.position = 0
with open(filepath) as f:
for line in f:
def get_object(self):
obj = self.entries[self.position]
self.position += 1
if self.position == len(self.entries):
self.position = 0
return obj
response_generator = None
class JSONHandler(tornado.web.RequestHandler):
def get(self):
self.set_header('Content-Type', 'application/json')
def bottle_handler():
return simplejson.dumps(response_generator.get_object())
if __name__ == '__main__':
global response_generator
parser = optparse.OptionParser()
parser.add_option('-i', '--input', help='file of lines to input')
opts, args = parser.parse_args()
if len(args) != 1:
parser.error('usage: [bottle|tornado]')
mode = args[0]
if mode not in ('tornado', 'bottle', 'bottle-paste', 'bottle-cherry'):
parser.error('usage: [bottle|bottle-cherry|bottle-paste|tornado]')
response_generator = ResponseGenerator(opts.input)
if mode == 'tornado':
application = tornado.web.Application([('/', JSONHandler)], debug=False)
elif mode == 'bottle':'localhost', port=8888)
elif mode == 'bottle-paste':, host='localhost', port=8888)
elif mode == 'bottle-cherry':, host='localhost', port=8888)
assert False