搞一个很小的web服务,需要用到全局变量。记录一下。
# -*- coding: utf-8 -*-
import sys,base64,json
import tornado.httpserver
import tornado.ioloop
import tornado.web
from tornado.options import define, options
define("apiserver", default="") #定义
options.parse_command_line()
class ApiHandler(tornado.web.RequestHandler):
def get(self):
self.write(options.apiserver) #读取
self.finish()
class SaveHandler(tornado.web.RequestHandler):
def get(self):
self.write(‘-1’)
def post(self):
options.apiserver = self.request.body #赋值
retval = {‘code’: 1}
self.write(json.dumps(retval))
self.finish()
application = tornado.web.Application([
(r"/api", ApiHandler),
(r"/save", SaveHandler),
])
if __name__ == "__main__":
http_server = tornado.httpserver.HTTPServer(application)
http_server.listen(88)
print ‘Demo is runing at 0.0.0.0:88\nQuit the demo with CONTROL-C’
tornado.ioloop.IOLoop.instance().start()