思路是这样的
思路: 1. 用SQLAlhemy 添加信息表和 用户表 2. 登陆时获取用户的用户名密码、 去数据库获取数据,并取得数据, 对比判断。 3. 查看所有主机,首先判断是否登陆。 如果登陆之后去数据库for 循环拉取数据 到页面。显示 4、 添加主机、 首先判断是否登陆,登陆之后,弹出一个添加主机的对话框、 判断所有添加的信息不能为空。如果为空就提示,并且数据的格式要固定。才能添加进去。 如果数据格式不正确。就用js 去判断。并且在后端代码中也进行判断
SQL 代码如下:
sql_main.py
#!/usr/bin/env python # -*- coding:utf-8 -*- # Author: liang import sqlalchemy from sqlalchemy import create_engine from sqlalchemy.ext.declarative import declarative_base from sqlalchemy import Column,String,Integer,DATE,CHAR,ForeignKey,VARCHAR from sqlalchemy.orm import sessionmaker,relationship # 数据库连接 engine=create_engine("mysql+pymysql://用户名:密码x@www.o2oxy.cn/liang", encoding="utf-8") # ROM 基类 Base=declarative_base() class UserHost(Base): __tablename__='user_host' id=Column(Integer,primary_key=True) # id ip=Column(VARCHAR(255),nullable=False) # ip port=Column(VARCHAR(255),nullable=False) # post user=Column(VARCHAR(255),nullable=False) # 用户 password=Column(VARCHAR(255),nullable=False) #密码 business=Column(VARCHAR(255),nullable=False) # 业务线 Asset_group=Column(VARCHAR(255),nullable=False) #资产组 Admin=Column(VARCHAR(255),nullable=False) # 管理员 def __repr__(self): return "<%s name:%s>" %(self.id,self.ip) class AdminUser(Base): __tablename__='admin_user' id=Column(Integer,primary_key=True) # id user=Column(String(32),nullable=False) # 用户名 password=Column(String(32),nullable=False) #密码 def __repr__(self): return "<%s name:%s>" %(self.id,self.user) Base.metadata.create_all(engine)
sql_insert.py
#!/usr/bin/env python # -*- coding:utf-8 -*- # Author: liang from cmdb import sql_mian from sqlalchemy.orm import sessionmaker Session_class=sessionmaker(bind=sql_mian.engine) Session=Session_class() ###创建 s2=sql_mian.UserHost(ip="192.168.10.1",port="3306",user="liang6",password="lioang",business="web",Asset_group="web",Admin="admin") s3=sql_mian.UserHost(ip="192.168.10.2",port="3306",user="lian8",password="lioang",business="web",Asset_group="web",Admin="admin") s4=sql_mian.UserHost(ip="192.168.10.3",port="3306",user="liang6",password="lioang",business="web",Asset_group="web",Admin="admin") s5=sql_mian.UserHost(ip="192.168.10.4",port="3306",user="liang5",password="lioang",business="web",Asset_group="web",Admin="admin") s6=sql_mian.UserHost(ip="192.168.10.5",port="3306",user="liang4",password="lioang",business="web",Asset_group="web",Admin="admin") s7=sql_mian.UserHost(ip="192.168.10.6",port="3306",user="liang3",password="lioang",business="web",Asset_group="web",Admin="admin") s8=sql_mian.UserHost(ip="192.168.10.7",port="3306",user="liang2",password="lioang",business="web",Asset_group="web",Admin="admin") s9=sql_mian.AdminUser(user="admin",password="admin123456") s10=sql_mian.AdminUser(user="admin",password="admin1234567899") Session.add_all([s2,s3,s4,s5,s6,s7,s8,s9,s10]) # x=Session.query(sql_mian.UserHost).filter_by(id=8).first() # # Session.delete(x) Session.commit()
sql_api.py
#!/usr/bin/env python # -*- coding:utf-8 -*- # Author: liang from cmdb import sql_mian from sqlalchemy.orm import sessionmaker import re Session_class=sessionmaker(bind=sql_mian.engine) Session=Session_class() def AdminUser(): obj = Session.query(sql_mian.AdminUser.user, sql_mian.AdminUser.password).all() return obj def UserHost(): obj = Session.query(sql_mian.UserHost.id, sql_mian.UserHost.ip, sql_mian.UserHost.port, sql_mian.UserHost.user, sql_mian.UserHost.password, sql_mian.UserHost.business, sql_mian.UserHost.Asset_group, sql_mian.UserHost.Admin).all() return obj def IsIP(str): p = re.compile('^((25[0-5]|2[0-4]d|[01]?dd?).){3}(25[0-5]|2[0-4]d|[01]?dd?)$') if p.match(str): return True else: return False def IsPort(number): if number >0 and number<65535: return True else: return False def LenGth(password,business,Asset_group,Admin): if len(password)<10 and len(business)<10 and len(Asset_group) and len(Admin): return True else: return False def AddUserHost(ip,port,user,password,business,Asset_group,Admin): ip2=IsIP(str(ip)) port2=int(port) port3=IsPort(port2) if ip2: if port3: if LenGth(password,business,Asset_group,Admin): s8=sql_mian.UserHost(ip=ip,port=port,user=user,password=password,business=business,Asset_group=Asset_group,Admin=Admin) Session.add_all([s8]) Session.commit() return True return False return False def DelUserHost(id): x = Session.query(sql_mian.UserHost).filter_by(id=id).first() if x: Session.delete(x) Session.commit() return True else: return False Session.commit()
首先是登陆页面
登陆成功之后呢
查看主机详情
可以添加主机
删除主机
源代码如下: