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')

  • 相关阅读:
    Luogu3118:[USACO15JAN]Moovie Mooving
    Luogu4137:Rmq Problem/mex
    Luogu3092:[USACO13NOV]No Change
    BZOJ4321: queue2
    BZOJ4650 : [NOI2016]优秀的拆分
    webpack
    sublime eslint setup
    Sublime themes/ lint themes setup
    sublime text 3
    React
  • 原文地址:https://www.cnblogs.com/zz-952/p/10546641.html
Copyright © 2011-2022 走看看