时间:2024-02-02 15:00:01 | 来源:网站运营
时间:2024-02-02 15:00:01 来源:网站运营
如何使用Python搭建一个OA网站?:1、学习一种web框架python -m pip install tornado
接下来,新建一个普通的python项目,并需要生成一些文件及文件夹,主要包括如下:from url import urlimport tornado.webimport ossettings = dict( template_path = os.path.join(os.path.dirname(__file__), "templates"), static_path = os.path.join(os.path.dirname(__file__), "statics"), debug=True #生产环境去掉 )application = tornado.web.Application( handlers = url, **settings )
2、server.py#!/usr/bin/env Python# coding=utf-8import tornado.ioloopimport tornado.optionsimport tornado.httpserverfrom application import applicationfrom tornado.options import define, optionsimport settingsdefine("port", default = 80, help = "run on the given port", type = int)def main(): #启动网站进程 tornado.options.parse_command_line() http_server = tornado.httpserver.HTTPServer(application) http_server.listen(options.port) host='127.0.0.1'; print ("Development server is running at http://%s:%s" % (host,options.port)) print ("Quit the server with Control-C") loop=tornado.ioloop.IOLoop.instance(); tornado.autoreload.start(loop); #更改代码无需重启时需要,只在开发环境 loop.start();if __name__ == "__main__": main()
3、url.py#!/usr/bin/env Python# coding=utf-8"""the url structure of website"""import sys #utf-8,兼容汉字from importlib import reloadreload(sys)from handlers.index import IndexHandlerurl = [ #网站各项功能 (r'/', IndexHandler) ]
说明:通过URL路径’/’,访问IndexHandler对应的HTML文件,通过上方import目录可以看到,这个文件在handlers文件夹里。关键词:使用