用python3.6+tornado4.4实现的剧本杀助手工具(三):【核心代码】
引用第三方库
import tornado.httpserver
import tornado.httpclient
import tornado.web
import tornado.websocket
import tornado.gen
import tornado.ioloop
import tornado.options
from tornado.escape import json_encode
import psycopg2
import pandas as pd
import time
import os
import hashlib
import logging
import pdb
设置kookie过期时长以及配置数据库连接参数
# ---config
cookie_time = 300000000 # cookie过期时延迟
# ---config
def conn_():
conn = psycopg2.connect(
database = "jubens",
user="postgres",
password="这个是密码,我不告诉你们",
host="127.0.0.1",
port="这个是端口,我也不告诉你们"
)
return conn
设置路由以及静态路径
# 设置路由以及静态路径
class Application(tornado.web.Application):
def __init__(self):
self.shoppingCart = ShoppingCart()
handlers = [
(r'/', IndexHandler) , # 首页
(r'/index', IndexHandler) , # 首页
]
settings = {
"cookie_secret": "bZJa3sWbeLKod7GkHn/VB3bXwQt8c0R0kRvJ1/xJf9E=",
"static_path": os.path.join(os.path.dirname(__file__), "static"),
"templates_path": os.path.join(os.path.dirname(__file__), "templates"),
"jubens_path": os.path.join(os.path.dirname(__file__), "jubens"),
}
tornado.web.Application.__init__(self, handlers, **settings)
渲染首页以及获取用户浏览器信息
# Index,就只有渲染首页的作用
class IndexHandler(tornado.web.RequestHandler):
@tornado.web.asynchronous
def get(self, *args, **kwargs):
headers = self.request.headers # 获取登陆浏览器头文件信息
mobile = ["(Android;", "(iPhone;", "(SymbianOS;", "(iPad;", "(iPod;", "(Windows;", "(Linux;"]
windows = ["(Windows"]
yes = 0 # 1为浏览器,0为移动端,2为其他。
self.render("templates/index.html")
def post(self, *args, **kwargs):
self.finish()
启动IO循环
if __name__ == "__main__":
port = 8000
print('webservers启动,http://127.0.0.1:' + str(port))
tornado.options.parse_command_line()
app = Application()
server = tornado.httpserver.HTTPServer(app)
server.listen(port)
tornado.ioloop.IOLoop.instance().start()
网友评论