Python MySQL数据导入

Python MySQL数据导入,python,mysql,json,mysql-python,python-db-api,Python,Mysql,Json,Mysql Python,Python Db Api,我使用下面的脚本从第三方工具中提取数据,在MySQL数据库中创建一个表,并用结果数据填充它。脚本运行完毕,我可以在pythonshell窗口中看到所有请求数据的打印输出。但是,当我打开数据库时,表是用列名创建的,但是没有行和数据。我四处搜索并阅读到,对于仅检索数据的脚本,我不需要使用“conn.commit”。这里是这样吗?如果没有,是否有人看到数据未填充表的另一个原因 import httplib2, urllib, json, pprint, getpass, string, time, M

我使用下面的脚本从第三方工具中提取数据,在MySQL数据库中创建一个表,并用结果数据填充它。脚本运行完毕,我可以在pythonshell窗口中看到所有请求数据的打印输出。但是,当我打开数据库时,表是用列名创建的,但是没有行和数据。我四处搜索并阅读到,对于仅检索数据的脚本,我不需要使用“conn.commit”。这里是这样吗?如果没有,是否有人看到数据未填充表的另一个原因

import httplib2, urllib, json, pprint, getpass, string, time, MySQLdb

def usage():
    print "Usage: python26 mysql.py or ./mysql.py"
    sys.exit(1)

if len(sys.argv) != 1:
    usage()

# Connect to the database and create the tables
conn = MySQLdb.connect (host = "localhost",
                   user = "XXXXXXXXX",
                   passwd = "XXXXXXXX")
cursor = conn.cursor ()
cursor.execute ("DROP DATABASE IF EXISTS tenable")
cursor.execute ("CREATE DATABASE tenable")
cursor.execute ("USE tenable")
cursor.execute ("""
CREATE TABLE cumvulndata
(
  offset         BIGINT(10),
  pluginName     TEXT,
  repositoryID   SMALLINT(3),
  severity       TINYINT(2),
  pluginID       MEDIUMINT(8),
  hasBeenMitigated   TINYINT(1),
  dnsName        VARCHAR(255),
  macAddress     VARCHAR(40),
  familyID       INT(4),
  recastRisk     TINYINT(1),
  firstSeen      DATETIME,
  ip             VARCHAR(15),
  acceptRisk     TINYINT(1),
  lastSeen       DATETIME,
  netbiosName    VARCHAR(255),
  port           MEDIUMINT(5),
  pluginText     MEDIUMTEXT,
  protocol       TINYINT(3)
  )
  """)
#
# Security Center organizational user creds
user = 'XXXXXXXXX'
passwd = 'XXXXXXXX'
url = 'https://Security Center Server/request.php'

def SendRequest(url, headers, data):
    http = httplib2.Http()
    response, content = http.request(url, 
                                    'POST', 
                                     headers=headers, 
                                     body=urllib.urlencode(data))
    if 'set-cookie' in response:
       headers['Cookie'] = response['set-cookie']
    return response, content

headers = {"Content-type": "application/x-www-form-urlencoded"}

input = {'password': passwd,
     'username': user}

# Convert input to login JSON
inputjson = json.dumps(input)

data = {"request_id": "8",
    "module": "auth",
    "action": "login",
    "input": inputjson}

# Send Login Request
response, content = SendRequest(url, headers, data)

# Decode JSON to python data structure
result = json.loads(content)

if result["error_code"] == 0:
    print "SC4 Login Successful"
    token = result['response']['token']
    print "Session Token:",token

# Construct the cumulative vuln query JSON 
cuminput = {'tool':'vulndetails',
     'startOffset':'0',
     'endOffset':sys.maxint,
     'sortField':'ip',
     'sortDir':'asc',
     'sourceType':'cumulative',
     'filters': [
                            {'filterName':'lastSeen',
                            'value':'31',
                            'operator':'<='},
                            {"filterName":"severity",
                            "value":"1,2,3",
                            "operator":"="}

            ]}
cuminputjson = json.dumps(cuminput)

 #
cumdata = {"request_id": "1",
        "module": "vuln",
    "action": "query",
    "input":cuminputjson,
        "token": token}

# Send the cumulative JSON and then populate the table
cumresponse, content = SendRequest(url, headers, cumdata)
resultc = json.loads(content)
off = 0
    print "\nFilling cumvulndata table with vulnerabilities from the cumulative    database.     Please wait..."
for result in resultc['response']['results']:
    off += 1
    cursor.execute ("""INSERT INTO cumvulndata   (offset,pluginName,repositoryID,severity,pluginID,hasBeenMitigated,dnsName,macAddress,familyID,recastRisk,firstSeen,ip,acceptRisk,lastSeen,netbiosName,port,pluginText,protocol)
    VALUES
    (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,(FROM_UNIXTIME(%s)),%s,%s,(FROM_UNIXTIME(%s)),%s,%s,%s,%s)""",   (off,result["pluginName"],result["repositoryID"],result["severity"],result["pluginID"],result["hasBeenMitigated"],result["dnsName"],result["macAddress"],result["familyID"],result["recastRisk"],result["firstSeen"],result["ip"],result["acceptRisk"],result["lastSeen"],result["netbiosName"],result["port"],result["pluginText"],result["protocol"]))


# Close the cursor and connection
cursor.close ()
conn.close ()

print "Done!!"
导入httplib2、urllib、json、pprint、getpass、string、time、MySQLdb
def用法():
打印“用法:python26 mysql.py或./mysql.py”
系统出口(1)
如果len(sys.argv)!=1:
用法()
#连接到数据库并创建表
conn=MySQLdb.connect(host=“localhost”,
user=“XXXXXXXXX”,
passwd=“XXXXXXXX”)
cursor=conn.cursor()
cursor.execute(“如果存在,则删除数据库并使其成立”)
cursor.execute(“创建数据库成立”)
cursor.execute(“使用可成立”)
cursor.execute(“”)
创建积云表
(
偏移量BIGINT(10),
pluginName文本,
repositoryID SMALLINT(3),
严重性TINYINT(2),
pluginID MEDIUMINT(8),
已将TINYINT(1)简化,
dnsName VARCHAR(255),
马卡德瑞斯·瓦查尔(40岁),
familyID INT(4),
重铸锡(1),
第一次见到日期时间,
ip VARCHAR(15),
接受风险TINYINT(1),
上次看到的日期时间,
netbiosName VARCHAR(255),
地中海港(5),
pluginText媒体文本,
TINYINT议定书(3)
)
""")
#
#安全中心组织用户信誉
用户='xxxxxxxx'
passwd='XXXXXXXX'
url='1〕https://Security 中心服务器/request.php'
def SendRequest(url、标题、数据):
http=httplib2.http()
响应,内容=http.request(url,
"岗位",,
标题=标题,
body=urllib.urlencode(数据))
如果响应中有“设置cookie”:
标题['Cookie']=响应['set-Cookie']
返回响应、内容
headers={“内容类型”:“application/x-www-form-urlencoded”}
输入={'password':passwd,
“用户名”:用户}
#将输入转换为登录JSON
inputjson=json.dumps(输入)
数据={“请求_id”:“8”,
“模块”:“身份验证”,
“操作”:“登录”,
“输入”:inputjson}
#发送登录请求
响应,内容=发送请求(url、标题、数据)
#将JSON解码为python数据结构
result=json.load(内容)
如果结果[“错误代码”]==0:
打印“SC4登录成功”
令牌=结果['response']['token']
打印“会话令牌:”,令牌
#构建累积vuln查询JSON
cuminput={'tool':'vulndetails',
“startOffset”:“0”,
“endOffset”:sys.maxint,
“sortField”:“ip”,
'sortDir':'asc',
'源类型':'累积',
“过滤器”:[
{'filterName':'lastSeen',
“值”:“31”,
“运算符”:“试试这个

import httplib2, urllib, json, pprint, getpass, string, time, MySQLdb
import sys
def usage():
    print "Usage: python26 mysql.py or ./mysql.py"
    sys.exit(1)

if len(sys.argv) != 1:
    usage()

# Connect to the database and create the tables
conn = MySQLdb.connect (host = "localhost",
                   user = "XXXXXXXXXX",
                   passwd = "XXXXXXXX")
cursor = conn.cursor ()
cursor.execute ("DROP DATABASE IF EXISTS tenable")
cursor.execute ("CREATE DATABASE tenable")
cursor.execute ("USE tenable")
cursor.execute ("""
CREATE TABLE cumvulndata
(
  offset         BIGINT(10),
  pluginName     TEXT,
  repositoryID   SMALLINT(3),
  severity       TINYINT(2),
  pluginID       MEDIUMINT(8),
  hasBeenMitigated   TINYINT(1),
  dnsName        VARCHAR(255),
  macAddress     VARCHAR(40),
  familyID       INT(4),
  recastRisk     TINYINT(1),
  firstSeen      DATETIME,
  ip             VARCHAR(15),
  acceptRisk     TINYINT(1),
  lastSeen       DATETIME,
  netbiosName    VARCHAR(255),
  port           MEDIUMINT(5),
  pluginText     MEDIUMTEXT,
  protocol       TINYINT(3)
  )
  """)
cursor.execute ("""INSERT INTO cumvulndata   (offset,pluginName,repositoryID,severity,pluginID,hasBeenMitigated,dnsName,macAddress,familyID,recastRisk,firstSeen,ip,acceptRisk,lastSeen,netbiosName,port,pluginText,protocol)
    VALUES
    (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)""",   ('123','plugin','10','1','12','1',"dnsName","macAddress",'15','1','2011-2-2',"ip",'9','2012-5-2',"netbiosName",'123',"pluginText","2"))

#Commit the changes.
conn.commit()
cursor.close()
conn.close()
请提交更改,然后您将获得插入的数据。

您的脚本不仅仅是“检索数据”——毕竟,您正在尝试将数据写入新表。