zoukankan      html  css  js  c++  java
  • Tornado创建一个web服务

    import tornado.web
    import tornado.ioloop
    import tornado.httpserver
    import tornado.options
    import config
    #全局参数定义
    tornado.options.define("port",default=8000,type = int, help="run server on the given port.")
    tornado.options.define("itcast",default=[],type = str, multiple = True, help="itcast subjects.")
    #一个请求对应RequestHandler信息和方法
    class IndexHandler(tornado.web.RequestHandler):
    	#http请求方式get
    	def get(self):
    		self.write("Hello world")
    		#http请求方式post
    	def post(self):
    		self.write("Hello world")
    
    
    if __name__ == '__main__':
    	print(tornado.options.options.itcast)
    	print(tornado.options.options.port)
    	#创建web应用实例对象  请求对应一个IndexHandler类
    	app = tornado.web.Application([
    		(r"/",IndexHandler),
    	], **config.settings)
    	# app.listen(8000)
    	#创建服务器实例,绑定服务器端口
    	http_server = tornado.httpserver.HTTPServer(app)
    	http_server.listen(tornado.options.options.port)
    	#启动当前线程的
    	tornado.ioloop.IOLoop.current().start()
    

      config.py

    import os
    # Tornado app配置
    settings = {
        'template_path': os.path.join(os.path.dirname(__file__), 'temp'),
        'static_path': os.path.join(os.path.dirname(__file__), 'statics'),
        'cookie_secret':'0Q1AKOKTQHqaa+N80XhYW7KCGskOUE2snCW06UIxXgI=',
        'xsrf_cookies':False,
        'login_url':'/login',
        'debug':True,
    }
    
    # 日志
    log_path = os.path.join(os.path.dirname(__file__), 'logs/log')

  • 相关阅读:
    最短路径问题
    这是我的第一篇博客
    Time Series Anomaly Detection
    R-CNN, Fast R-CNN, Faster R-CNN, Mask R-CNN
    database 学习
    AWS Cloud Practioner 官方课程笔记
    Cassandra commands
    go 语言学习
    [Udemy] ES 7 and Elastic Stack
    第9章 内联函数
  • 原文地址:https://www.cnblogs.com/zz-952/p/10546641.html
Copyright © 2011-2022 走看看