diff --git a/README.md b/README.md index 468cbc3..24b4bcf 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,13 @@ # 更新日志 +* 2021年1月22日,进一步完善服务器基本框架。 + * 基本尝试使用了BokehJS库的使用,计划用JS替代Python版本的Bokeh功能控件。 + * 尝试Plotly的使用,计划作为Bokeh的备选。 + * 尝试使用Javascript的Jquery库,利用ajax向服务器发送post请求传递信息。 + * 发现Python版本的Bokeh功能控件部署在服务器上存在的一些问题,包括后台终端无后台信息等,暂时将该功能控件禁用。 + * 新增Mongo数据库的使用,计划用来记录用户的行为数据;该数据库的部署已经完成;该数据库的python封装已完成雏形,但尚不完善,需要后续根据采集节点的设计进一步完善。 + * 后续需要在项目管理方面进行梳理,当前代码库已经存在一定的混乱。 * 2021年1月20日,完成了服务器基本框架的搭建。实现的功能有: * 用户登录、认证、退出系统,新用户注册新账户 * 在mysql数据库中存储、更新用户基本信息 diff --git a/Server/app.py b/Server/app.py index a7533bd..ff346d8 100644 --- a/Server/app.py +++ b/Server/app.py @@ -2,9 +2,9 @@ import tornado.ioloop #开启循环,让服务一直等待请求的到来 import tornado.web #web服务基本功能都封装在此模块中 import tornado.options #从命令行中读取设置 from tornado.options import define,options #导入包 -from handler import main, auth +from handler import main, auth, spice # import _thread,threading -from spice import app_spice +# from spice import app_spice define('port',default='8000',help='Listening port',type=int) #定义如何接受传进来的东西 @@ -16,7 +16,11 @@ class Application(tornado.web.Application): #引入Application类,重写方 (r'/login',auth.LoginHandler), (r'/logout',auth.LogoutHandler), (r'/register',auth.RegisterHandler), - (r'/spice',main.Spice_Xyce_Handler), + # (r'/spice',spice.Spice_1_Handler), + (r'/spice2',spice.Spice_2_Handler), + (r'/spice3',spice.Spice_3_Handler), + (r'/spice4',spice.Spice_4_Handler), + (r'/test',main.TestHandler), ] settings = dict( debug = False, #调试模式,修改后自动重启服务,不需要自动重启,生产情况下切勿开启,安全性 @@ -50,7 +54,7 @@ app = Application() #实例化 if __name__ == '__main__': #当.py文件被直接运行时,代码块将被运行;当.py文件以模块形式被导入时,代码块不被运行。 # 开启 bokeh 服务 - app_spice.app_spice_begin() + # app_spice.app_spice_begin() tornado.options.parse_command_line() app.listen(options.port) ##如果一个与define语句中同名的设置在命令行中被给出,那么它将成为全局的options的一个属性 即 options.port 相当于define的url的port diff --git a/Server/handler/MongoDB.py b/Server/handler/MongoDB.py new file mode 100644 index 0000000..9ea4ab3 --- /dev/null +++ b/Server/handler/MongoDB.py @@ -0,0 +1,31 @@ +from pymongo import MongoClient +import datetime + + +class Mongo_DB(): + + def __init__(self, Mongo_URL = 'mongodb://localhost:27017/'): + self.Client = MongoClient(Mongo_URL) + + def connect(self,DataBase = 'default', Collection = 'default'): + self.DataBase = self.Client[DataBase] + self.Collection = self.DataBase[Collection] + + def __insert(self, contents ): + if type(contents) == list: + self.Collection.insert_many(contents) + elif type(contents) == dict: + self.Collection.insert_one(contents) + else: + print("Data's format error !!!") + + + def update(self, behavior = '' , spice = '', tags = ''): + message = {} + message['behavior'] = behavior + message['spice'] = spice + message['tags'] = tags + message['date'] = datetime.datetime.utcnow() + self.__insert(message) + +Mongo = Mongo_DB() \ No newline at end of file diff --git a/Server/handler/database.py b/Server/handler/MysqlDB.py similarity index 95% rename from Server/handler/database.py rename to Server/handler/MysqlDB.py index b9385b5..abf9717 100644 --- a/Server/handler/database.py +++ b/Server/handler/MysqlDB.py @@ -10,7 +10,7 @@ table_name = "user_info" # 打开数据库连接 db = pymysql.connect( database_ip, database_user, database_passwd, database_name ) - +print('open mysql success !!!') # 使用cursor()方法获取操作游标 cursor = db.cursor() diff --git a/Server/handler/__pycache__/MongoDB.cpython-36.pyc b/Server/handler/__pycache__/MongoDB.cpython-36.pyc new file mode 100644 index 0000000..992e607 Binary files /dev/null and b/Server/handler/__pycache__/MongoDB.cpython-36.pyc differ diff --git a/Server/handler/__pycache__/MysqlDB.cpython-36.pyc b/Server/handler/__pycache__/MysqlDB.cpython-36.pyc new file mode 100644 index 0000000..25bcf29 Binary files /dev/null and b/Server/handler/__pycache__/MysqlDB.cpython-36.pyc differ diff --git a/Server/handler/__pycache__/account.cpython-36.pyc b/Server/handler/__pycache__/account.cpython-36.pyc index fcf7bc5..fd74217 100644 Binary files a/Server/handler/__pycache__/account.cpython-36.pyc and b/Server/handler/__pycache__/account.cpython-36.pyc differ diff --git a/Server/handler/__pycache__/auth.cpython-36.pyc b/Server/handler/__pycache__/auth.cpython-36.pyc index 007d6ee..26f0316 100644 Binary files a/Server/handler/__pycache__/auth.cpython-36.pyc and b/Server/handler/__pycache__/auth.cpython-36.pyc differ diff --git a/Server/handler/__pycache__/main.cpython-36.pyc b/Server/handler/__pycache__/main.cpython-36.pyc index a8e9fb8..0c2136b 100644 Binary files a/Server/handler/__pycache__/main.cpython-36.pyc and b/Server/handler/__pycache__/main.cpython-36.pyc differ diff --git a/Server/handler/__pycache__/spice.cpython-36.pyc b/Server/handler/__pycache__/spice.cpython-36.pyc new file mode 100644 index 0000000..3729cf3 Binary files /dev/null and b/Server/handler/__pycache__/spice.cpython-36.pyc differ diff --git a/Server/handler/account.py b/Server/handler/account.py index d85e191..0ca8957 100644 --- a/Server/handler/account.py +++ b/Server/handler/account.py @@ -1,6 +1,5 @@ import datetime -from .database import * - +from .MysqlDB import * #用户密码匹配判断函数 def authenticate(username,password): diff --git a/Server/handler/auth.py b/Server/handler/auth.py index 8522075..36929e7 100644 --- a/Server/handler/auth.py +++ b/Server/handler/auth.py @@ -1,9 +1,10 @@ from .main import AuthBaseHandler from .account import authenticate, add_user +from .MongoDB import * class LoginHandler(AuthBaseHandler): def get(self,*args,**kwargs): - self.render('login.html') + self.render('auth/login.html') def post(self,*args,**kwargs): username = self.get_argument('username',None) @@ -15,11 +16,15 @@ class LoginHandler(AuthBaseHandler): if passed: # 保存cookie信息到redis数据库 self.session.set('username',username) #将前面设置的cookie设置为username,保存用户登录信息 - print(self.session.get('username')) + print(self.session.get('username')+' login success !!!') next_url = self.get_argument('next', '') # 获取之前页面的路由 if next_url: + Mongo.connect(DataBase='example',Collection=username) + Mongo.update(behavior='login',tags='auth') self.redirect(next_url) #跳转主页路由 else: + Mongo.connect(DataBase='example',Collection=username) + Mongo.update(behavior='login',tags='auth') self.redirect('/') else: self.write({'msg':'login fail'}) #不通过,有问题 @@ -28,6 +33,8 @@ class LoginHandler(AuthBaseHandler): class LogoutHandler(AuthBaseHandler): def get(self, *args, **kwargs): + Mongo.connect(DataBase='example',Collection=self.get_current_user()) + Mongo.update(behavior='logout',tags='auth') #self.session.set('username','') #将用户的cookie清除 self.session.delete('username') self.redirect('/login') @@ -39,7 +46,7 @@ class LogoutHandler(AuthBaseHandler): class RegisterHandler(AuthBaseHandler): def get(self, *args, **kwargs): print('register') - self.render('register.html') + self.render('auth/register.html') def post(self, *args, **kwargs): print('registerpost') @@ -51,9 +58,11 @@ class RegisterHandler(AuthBaseHandler): if username and password1 and (password1 == password2): success = add_user(username,password1) if success: + Mongo.connect(DataBase='example',Collection=username) + Mongo.update(behavior='register',tags='auth') self.redirect('/login') else: self.write({'msg':'register fail'}) else: print('register again') - self.render('register.html') \ No newline at end of file + self.render('auth/register.html') \ No newline at end of file diff --git a/Server/handler/main.py b/Server/handler/main.py index 32a9f23..b5ee32b 100644 --- a/Server/handler/main.py +++ b/Server/handler/main.py @@ -1,30 +1,30 @@ import tornado.web from pycket.session import SessionMixin -from bokeh.embed import server_document -from jinja2 import Environment, FileSystemLoader +from .MongoDB import * -env = Environment(loader=FileSystemLoader('template')) class AuthBaseHandler(tornado.web.RequestHandler,SessionMixin): def get_current_user(self): #重写get_current_user()方法 return self.session.get('username',None) #session是一种会话状态,跟数据库的session可能不一样 - + #添加装饰器,装饰需要验证的请求 class IndexHandler(AuthBaseHandler): @tornado.web.authenticated #@tornado.web.authenticated装饰器包裹get方法时,表示这个方法只有在用户合法时才会调用,authenticated装饰器会调用get_current_user()方法获取current_user的值,若值为False,则重定向到登录url装饰器判断有没有登录,如果没有则跳转到配置的路由下去,但是要在app.py里面设置login_url def get(self,*args,**kwargs): - user = self.get_current_user() - self.render('index.html',user=user) + username = self.get_current_user() + self.render('index.html',user=username) +class TestHandler(AuthBaseHandler): + + @tornado.web.authenticated #@tornado.web.authenticated装饰器包裹get方法时,表示这个方法只有在用户合法时才会调用,authenticated装饰器会调用get_current_user()方法获取current_user的值,若值为False,则重定向到登录url装饰器判断有没有登录,如果没有则跳转到配置的路由下去,但是要在app.py里面设置login_url + def post(self,*args,**kwargs): + username = self.get_current_user() + message = self.get_argument('message') -class Spice_Xyce_Handler(AuthBaseHandler): + print('TestHandler: '+username+' '+message) + Mongo.connect(DataBase='example',Collection=username) + Mongo.update(behavior=message,tags='test') - @tornado.web.authenticated - def get(self,*args,**kwargs): - template = env.get_template('spice.html') - script = server_document('http://localhost:5006/bkapp') - self.write(template.render(script=script)) + self.write("success") - # script = server_document('http://localhost:5006/bkapp') - # self.render('spice.html',script=script) diff --git a/Server/handler/spice.py b/Server/handler/spice.py index e69de29..d5320a8 100644 --- a/Server/handler/spice.py +++ b/Server/handler/spice.py @@ -0,0 +1,99 @@ +from .main import AuthBaseHandler +import tornado.web +from bokeh.embed import server_document +# from jinja2 import Environment, FileSystemLoader + +class Spice_1_Handler(AuthBaseHandler): + + @tornado.web.authenticated + def get(self,*args,**kwargs): + # env = Environment(loader=FileSystemLoader('template')) + # template = env.get_template('spice.html') + # script = server_document('http://localhost:5006/bkapp') + # self.write(template.render(script=script)) + + script = server_document('http://localhost:5006/bkapp') + self.render('spice/spice1.html',script=script) + +class Spice_2_Handler(AuthBaseHandler): + + @tornado.web.authenticated + def get(self,*args,**kwargs): + js_import = """ + + + + + + + """ + js_code = """ + + """ + self.render('spice/spice2.html',js_import=js_import,js_code=js_code) + + +class Spice_3_Handler(AuthBaseHandler): + @tornado.web.authenticated + def get(self,*args,**kwargs): + self.render('spice/spice3.html') + + +class Spice_4_Handler(AuthBaseHandler): + @tornado.web.authenticated + def get(self,*args,**kwargs): + self.render('spice/spice4.html') \ No newline at end of file diff --git a/Server/spice/__pycache__/app_spice.cpython-36.pyc b/Server/spice/__pycache__/app_spice.cpython-36.pyc index 03e4910..ba03993 100644 Binary files a/Server/spice/__pycache__/app_spice.cpython-36.pyc and b/Server/spice/__pycache__/app_spice.cpython-36.pyc differ diff --git a/Server/template/login.html b/Server/template/auth/login.html similarity index 100% rename from Server/template/login.html rename to Server/template/auth/login.html diff --git a/Server/template/register.html b/Server/template/auth/register.html similarity index 100% rename from Server/template/register.html rename to Server/template/auth/register.html diff --git a/Server/template/index.html b/Server/template/index.html index d845b24..b845db5 100644 --- a/Server/template/index.html +++ b/Server/template/index.html @@ -11,6 +11,15 @@
+
+
+
+
+
+
+
+
+
diff --git a/Server/template/spice/spice1.html b/Server/template/spice/spice1.html new file mode 100644 index 0000000..ca58ea2 --- /dev/null +++ b/Server/template/spice/spice1.html @@ -0,0 +1,20 @@ + + + + + + Embedding a Bokeh Server With + + + +
+ This Bokeh app below served by a Bokeh server that has been embedded + in another web app framework. For more information see the section + Embedding Bokeh Server as a Library + in the User's Guide. +
+
+ {% raw script %} +
+ + diff --git a/Server/template/spice.html b/Server/template/spice/spice1_jinja.html similarity index 100% rename from Server/template/spice.html rename to Server/template/spice/spice1_jinja.html diff --git a/Server/template/spice/spice2.html b/Server/template/spice/spice2.html new file mode 100644 index 0000000..1ead21e --- /dev/null +++ b/Server/template/spice/spice2.html @@ -0,0 +1,17 @@ + + + + + + Complete Example + + {% raw js_import %} + + + +
+ {% raw js_code %} +
+ + + diff --git a/Server/template/spice/spice3.html b/Server/template/spice/spice3.html new file mode 100644 index 0000000..ad53e34 --- /dev/null +++ b/Server/template/spice/spice3.html @@ -0,0 +1,95 @@ + + + + + + Complete Example + + + + + + + + + +
+ + +
+ +
+ +
+ + + diff --git a/Server/template/spice/spice4.html b/Server/template/spice/spice4.html new file mode 100644 index 0000000..b75d696 --- /dev/null +++ b/Server/template/spice/spice4.html @@ -0,0 +1,31 @@ + + + + + + + + + + + +