base_project/
--|application/
----|__init__.py
----|config.py
----|views/
------|index.py
----|templates/
----|static/
--|server.py
application/config.py
import os
BASE_DIR = os.path.dirname(__file__)
options = {
"port": 8000
}
settings = {
"debug": True,
"template_path": os.path.join(BASE_DIR, "templates"),
"static_path": os.path.join(BASE_DIR, "static")
}
application/views/index.py
import tornado.web
class IndexHandler(tornado.web.RequestHandler):
def get(self):
self.write("hello index")
application/__init__.py
import tornado.web
import tornado.ioloop
from application import config
from application.views import index
class Application(tornado.web.Application):
def __init__(self):
handlers = [
(r"/", index.IndexHandler)
]
super(Application, self).__init__(handlers, **config.settings)
server.py
from application import Application
from application import config
import tornado.ioloop
if __name__ == "__main__":
app = Application()
app.listen(config.options["port"])
tornado.ioloop.IOLoop.current().start()
启动项目
python server.py
浏览器访问http://127.0.0.1:8000
即可访问项目
网友评论