tornado简单使用
test_tornado.py
# coding: utf-8
from __future__ import unicode_literals
import tornado
import tornado.web
class MyHandler(tornado.web.RequestHandler):
def initialize(self, param1):
self._param1 = param1
def post(self):
arg1 = self.get_argument("arg1", strip=False)
arg2 = self.get_argument("arg2")
result = "param1: {}, arg1: {}, arg2: {}".format(self._param1, arg1, arg2)
self.write(result)
print(result)
if __name__ == '__main__':
param1 = "abc"
app = tornado.web.Application([('/test', MyHandler, {"param1": param1}), ])
app.listen(address="0.0.0.0", port=8000)
tornado.ioloop.IOLoop.current().start()
执行下面命令启动server:
python test_tornado.py
使用curl 测试服务:
curl -d "arg1= def &arg2= ghi " http://127.0.0.1:8000/test
你会得到下面结果:
param1: abc, arg1: def , arg2: ghi