学习

【Python】Flask系列-数据库笔记

实践

#!/usr/bin/env python
# -*- coding: utf-8 -*-
from sqlalchemy import create_engine
from sqlalchemy.schema import Column
from sqlalchemy.types import Integer, DateTime, LargeBinary, Text, String
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
import datetime ,time import json
import binascii
Base = declarative_base() class SqlPlugin(object):
class Reports(Base):
"""
ORM映射子域名的嵌入类
简单的列表
"""
__tablename__ = 'result_siblings' # 建立表列 id = Column('id', Integer, primary_key=True) # 自动增长ID
host = Column('host', String(256)) # 主机,目标域名表对应的IP做主要关联
title = Column('title', String(256)) # 网站标题
ip = Column('ip', String(256)) # IP
domain = Column('domain', String(256)) # 子域名
port = Column('port', String(256)) # 当前HTTP网络端口
country = Column('country', String(256)) # 国家代码
province = Column('province', String(256)) # 省份
city = Column('city', String(256)) # 城市
country_name = Column('country_name', String(256)) # 国家名字
header = Column('header', String(256)) # 网络回显
server = Column('server', String(256)) # 容器版本
protocol = Column('protocol', String(256)) # 协议
banner = Column('banner', String(256)) # 版本信息
cert = Column('cert', String(5000)) # 证书信息
isp = Column('isp', String(256)) # 运营商
as_number = Column('as_number', String(256)) # Autonomous System (AS) Numbers
as_organization = Column('as_organization', String(256)) # as_organization
data_source = Column('data_source', String(256)) # 数据来源
app_name = Column('app_name', String(256)) # 应用指纹
create_time = Column('create_time', DateTime(), default='now') # 数据创建时间
update_time = Column('update_time', DateTime(), default='now') # 数据更新时间 # 通过构造函数写入内容
def __init__(self, insert_value):
dt = datetime.datetime.now()
dt_now = dt.strftime('%Y-%m-%d %H:%M:%S') # 当前日期和当前时间
self.update_time = dt_now # 数据更新时间
self.create_time = dt_now # 数据创建时间 self.host = insert_value['host'] # 主机,目标域名表对应的IP做主要关联
self.title = insert_value['title'] # 网站标题
self.ip = insert_value['ip'] # IP
self.domain = insert_value['domain'] # 子域名
self.port = insert_value['port'] # 当前HTTP网络端口
self.country = insert_value['country'] # 国家代码
self.province = insert_value['province'] # 省份
self.city = insert_value['city'] # 城市
self.country_name = insert_value['country_name'] # 国家名字
self.header = insert_value['header'] # 网络回显
self.server = insert_value['server'] # 容器版本
self.protocol = insert_value['protocol'] # 协议
self.banner = insert_value['banner'] # 版本信息
self.cert = insert_value['cert'] # 证书信息
self.isp = insert_value['isp'] # 运营商
self.as_number = insert_value['as_number'] # Autonomous System (AS) Numbers
self.as_organization = insert_value['as_organization'] # as_organization
self.data_source = insert_value['data_source'] # 数据来源
self.app_name = insert_value['app_name'] # 应用指纹 def __init__(self, **kwargs):
"""
constructor receive a **kwargs as the **kwargs in the sqlalchemy
create_engine() method (see sqlalchemy docs)
You must add to this **kwargs an 'url' key with the url to your
database
This constructor will :
- create all the necessary obj to discuss with the DB
- create all the mapping(ORM) todo : suport the : sqlalchemy.engine_from_config :param **kwargs:
:raises: ValueError if no url is given,
all exception sqlalchemy can throw
ie sqlite in memory url='sqlite://' echo=True
ie sqlite file on hd url='sqlite:////tmp/reportdb.sql' echo=True
ie mysql url='mysql+mysqldb://scott:tiger@localhost/foo'
"""
# SqlPlugin.__init__(self)
self.engine = None
self.url = None
self.Session = sessionmaker() if 'url' not in kwargs:
raise ValueError
self.url = kwargs['url']
del kwargs['url'] # 建立库
try:
self.engine = create_engine(self.url, **kwargs) # 链接数据库
Base.metadata.create_all(bind=self.engine, checkfirst=True) # 按照子类的结构在数据库中生成对应的数据表信息
self.Session.configure(bind=self.engine) # 将创建的数据库连接关联到这个session
except:
raise def insert(self, insert_value):
"""
在后端插入子域名的数据
:returns: 后端对象的标识以供将来使用或者None
""" # 写入当前扫描的信息
sess = self.Session()
report = SqlPlugin.Reports(insert_value) # 把字典参数写入到类的构造函数里
sess.add(report) # 添加要添加的参数
sess.commit() # 提交数据
reportid = report.id # 返回插入到数据库里的索引ID
sess.close() return reportid if reportid else None def get_filter_domain(self, domain=None):
if domain is None:
raise ValueError
sess = self.Session()
# 获取指定地址的所有端口
out_report = (
sess.query(SqlPlugin.Reports).filter_by(domain=domain).first())
sess.close()
# 查询成功返回值
if out_report == None:
return False
else:
return True def update(self, update_value):
sess = self.Session()
update_info = (
sess.query(SqlPlugin.Reports).filter_by(domain=update_value['domain']).first()) # 查询指定IP,指定端口 # 更新数据日期
dt = datetime.datetime.now()
dt_now = dt.strftime('%Y-%m-%d %H:%M:%S') # 当前日期和当前时间
update_info.update_time = dt_now
update_info.title = update_value['title'] # 修改标题
update_info.ip = update_value['ip'] # 修改IP
update_info.domain = update_value['domain'] # 修改domain
update_info.port = update_value['port'] # 修改port
update_info.country = update_value['country'] # 修改country
update_info.province = update_value['province'] # 修改province
update_info.city = update_value['city'] # 修改city
update_info.country_name = update_value['country_name'] # 修改country_name
update_info.header = update_value['header'] # 修改header
update_info.server = update_value['server'] # 修改server
update_info.protocol = update_value['protocol'] # 修改protocol
update_info.banner = update_value['banner'] # 修改banner
update_info.cert = update_value['cert'] # 修改cert
update_info.isp = update_value['isp'] # 修改isp
update_info.as_number = update_value['as_number'] # 修改as_number
update_info.as_organization = update_value['as_organization'] # 修改as_organization
update_info.data_source = update_value['data_source'] # 修改data_source
update_info.app_name = update_value['app_name'] # 修改app_name sess.commit()
sess.close() def get(self, report_id=None):
"""
获取指定ID的数据 :param id: str :returns: 子域名单条数据
"""
if report_id is None:
raise ValueError
sess = self.Session()
our_report = (
sess.query(SqlPlugin.Reports).filter_by(id=report_id).first())
sess.close()
return our_report.decode() if our_report else None def getall(self):
"""
:param filter:实现了一个过滤器功能 :returns: 元组集合(id,子域名数据)
"""
sess = self.Session()
nmapreportList = [] for report in (
sess.query(SqlPlugin.Reports).
order_by(SqlPlugin.Reports.inserted)):
nmapreportList.append((report.id, report.decode()))
sess.close() return nmapreportList def delete(self, report_id=None):
"""
从后端删除报表 :param id: 字符串 :returns: 删除的行数
"""
if report_id is None:
raise ValueError
nb_line = 0
sess = self.Session()
nb_line = sess.query(SqlPlugin.Reports). \
filter_by(id=report_id).delete()
sess.commit()
sess.close()
return nb_line if __name__ == "__main__":
sql_opreration = SqlPlugin(url='mysql+pymysql://root:root@192.168.221.143:3306/17bdw_data?charset=utf8', echo=False,
encoding='utf-8', pool_timeout=3600)
# 数据
insert_value = {"host": 1,
"title": 2,
"ip": 3,
"domain": 4,
"port": 5,
"country": 6,
"province": 7,
"city":8,
"country_name":9,
"header":10,
"server":11,
"protocol":12,
"banner":13,
"cert":14,
"isp":15,
"as_number":16,
"as_organization":17,
"data_source":18,
"app_name":200
} # 判断是否存在这个值来决定是插入还是更新
if sql_opreration.get_filter_domain(domain=123):
sql_opreration.update(insert_value) # 更新数据
else:
sql_opreration.insert(insert_value) # 插入数据

python扫描器-sqlalchemy入库操作的更多相关文章

  1. 基于Python的SQLAlchemy的操作

    安装 在Python使用SQLAlchemy的首要前提是安装相应的模块,当然作为python的优势,可以到python安装目录下的scripts下,同时按住shift+加上鼠标左键,从而在菜单中打开命 ...

  2. python之SQLAlchemy ORM 上

    前言: SQLAlchmey是暑假学的,当时学完后也没及时写博客整理下.这篇博客主要介绍下SQLAlchemy及基本操作,写完后有空做个堡垒机小项目.下篇博客整理写篇关于Web框架和django基础~ ...

  3. python之SQLAlchemy ORM

    前言: 这篇博客主要介绍下SQLAlchemy及基本操作,写完后有空做个堡垒机小项目.有兴趣可看下python之数据库(mysql)操作.下篇博客整理写篇关于Web框架和django基础~~ 一.OR ...

  4. 第二百八十九节,MySQL数据库-ORM之sqlalchemy模块操作数据库

    MySQL数据库-ORM之sqlalchemy模块操作数据库 sqlalchemy第三方模块 sqlalchemysqlalchemy是Python编程语言下的一款ORM框架,该框架建立在数据库API ...

  5. python使用sqlalchemy连接pymysql数据库

    python使用sqlalchemy连接mysql数据库 字数833 阅读461 评论0 喜欢1 sqlalchemy是python当中比较出名的orm程序. 什么是orm? orm英文全称objec ...

  6. sqlalchemy 的操作

    使用PyMySQL的前提: 1. 先建好表 2. 自己动手需要手动去写多条SQL语句 改进: 类 ------>  表 实例化 -> 数据 这种思想叫:ORM(Object Relatio ...

  7. Python开发【第三篇】:Python基本之文件操作

    Python基本之文本操作 一.初识文本的基本操作 在python中打开文件有两种方式,即:open(...) 和  file(...) ,本质上前者在内部会调用后者来进行文件操作,推荐使用 open ...

  8. 【Python数据分析】Python3操作Excel(二) 一些问题的解决与优化

    继上一篇[Python数据分析]Python3操作Excel-以豆瓣图书Top250为例 对豆瓣图书Top250进行爬取以后,鉴于还有一些问题没有解决,所以进行了进一步的交流讨论,这期间得到了一只尼玛 ...

  9. [python]用Python进行SQLite数据库操作

    用Python进行SQLite数据库操作 1.导入Python SQLITE数据库模块 Python2.5之后,内置了SQLite3,成为了内置模块,这给我们省了安装的功夫,只需导入即可~  ]: u ...

随机推荐

  1. 【概率论】5-10:二维正态分布(The Bivariate Normal Distributions)

    title: [概率论]5-10:二维正态分布(The Bivariate Normal Distributions) categories: - Mathematic - Probability k ...

  2. 洛谷P2456 二进制方程

    题目 字符串模拟+并查集 建立两个并查集分别存放每个变量的每一位数的祖先,一个是1一个是2 考虑每个字母的每一位的数都是唯一的,先模拟,记录每一个变量的每一位. 一一映射到方程中去,最后将两个方程进行 ...

  3. 数据结构实验之排序六:希尔排序 (SDUT 3403)

    其实,感觉好像增量不同的冒泡,希尔排序概念以后补上. #include <bits/stdc++.h> using namespace std; int a[10005]; int b[1 ...

  4. deepin 深度Linux系统 15.11 链接蓝牙鼠标问题

    不知道为毛就是搜索不到,好吧只能用老方法,那就是不使用deepin系统自带的面板进行管理 用下面的命令进行安装配置即可 sudo apt install bluetooth blueman bluem ...

  5. Semantic Segmentation on Remotely Sensed Images Using an Enhanced Global Convolutional Network with Channel Attention and Domain Specific Transfer Learning

    创新点: 1.在GCN(global convolutional network)基础上,把他的backbone替换成更多层的,使其适应中分辨率影像,resnet50,101,152 2.利用 cha ...

  6. Selenium向iframe富文本框输入内容

    目录 前言 只输入纯文本 通过JS注入HTML代码 前言 在使用Selenium测试一些CMS后台系统时,有时会遇到一些富文本框,如下图所示: 整个富文本编辑器是通过iframe嵌入到网页中的,手动尝 ...

  7. computed的用法

    其实在摸板中也是可以做简单的计算的,但是会看起来会很乱 ,可以用computed来做计算 <!DOCTYPE html> <html lang="en"> ...

  8. 1、ES6声明变量的方式

    1.es5的声明方式var的问题 1)是var在同一个作用域内部,是可以重复声明一个变量的,后面的会把前面的覆盖掉 2)var存在变量提升到的问题,就是在使用var声明变量前是可以先使用此变量的 2. ...

  9. python技巧获取26个英语字母

    import string string.ascii_uppercase # 获取26个大写字母 string.ascii_lowercase # 获取26个小写字母 string.ascii_let ...

  10. [C++] wchar_t关键字使用方法

    char 是单字符类型,长度为一个字节 wchar_t 是宽字符类型,长度为两个字节,主要用在国际 Unicode 编码中 举例: #include<iostream> using nam ...