- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有以下连接到 MySQL 数据库的代码,并检查哪些记录被包含“1”的字段标记为“事件”。
代码然后使用下载 URL 中 vulntype
字段的内容下载一些文件。
我认为问题在于 MySQL 查询或执行下载的 for 循环。
代码是:-
import requests
import os
import MySQLdb
from hurry.filesize import size, si
import logging
import logging.handlers
from logging.config import fileConfig
logging.handlers = logging.handlers
fileConfig('data/logging_config.ini')
logger = logging.getLogger("downloader")
active_vuln_type = None
def get_active_vuln_sets():
global active_vuln_type
try:
logging.info('Connecting to the database...')
active_vuln_type = con = MySQLdb.connect(*******)
logging.info('Database connected!')
except FileNotFoundError as fnf:
logging.error(fnf)
except MySQLdb.Error as e:
logging.error(e)
try:
logging.info('Getting active vulnerability sets...')
cur = con.cursor()
active = "1"
cur.execute("""SELECT vulntype FROM vuln_sets WHERE active = %s""", (active))
active_vuln_type = cur.fetchall()
except MySQLdb.Error as e:
logging.exception(e)
def download():
try:
logging.info('Downloading vulnerability set files...')
for x in active_vuln_type:
basepath = os.path.dirname(__file__)
filepath = os.path.abspath(os.path.join(basepath, ".."))
response = requests.get('https://vulners.com/api/'
'v3/archive/collection/?type=' + x)
with open(filepath + '/vuln_files/' + x + '.zip', 'wb') as f:
f.write(response.content)
filesize = size(os.path.getsize
(filepath + '/vuln_files/'
+ x + '.zip'), system=si)
files = x + ".zip - " + str(filesize)
logging.info('Downloaded ' + x + '.zip Successfully')
logging.info('File details: ' + files)
except Exception as e:
logging.exception(e)
回溯是:-
Traceback (most recent call last):
File "/home/luke/projects/vuln_backend/vuln_backend/download.py", line 61, in download
'v3/archive/collection/?type=' + x)
TypeError: must be str, not tuple
最佳答案
active_vuln_type = cur.fetchall()
此行返回数据库中的行列表。 每一行都是一个元组。当然,您只是从表中选择一列,但界面是相同的:每一行都是一个元组,每列一个值。
for x in active_vuln_type:
这里的 x
是一个类似 ("vulnerability of some kind",)
的元组;注意尾随的逗号。你需要解压它:
for db_row in active_vuln_type:
x = db_row[0] # The first and column in the row.
除此之外,请考虑描述性地命名 x
,从一个过程返回 active_vuln_type
并将其作为参数传递给另一个过程。这将使您的代码不那么脆弱并且更易于测试。
def get_active_vuln_sets(db_host, db_user, db_password):
# Do your stuff, using the db credentials....
return active_vuln_type
def download(active_vuln_type):
# Same source as now.
现在您可以下载(get_active_vuln_sets('192.168.100.1', 'joe', 'secret'))
或者您可以在不接触数据库的情况下测试/重试相同的操作:download([("CVE-xyxxy",), ("CVE-asdfghjkl",)])
。
您可以做的另一件事是返回一个干净的漏洞名称列表,而不是原始数据库元组:
def get_active_vuln_sets(...):
# .....
return [x[0] for x in cur.fetchall()]
现在返回的值将是一个单一的、可直接使用的值的列表,因此您的原始 download(...)
代码可以使用它。
关于Python MySQL TypeError : must be str, 不是元组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46913841/
typing模块中使用List、Tuple等有什么区别: from typing import Tuple def f(points: Tuple): return map(do_stuff,
如何遍历列表的每 5 个元素并将它们组成一个元组,然后将同一列表的第 6 个元素作为第二个元组 - 然后对接下来的 5 个元素和第 6 个元素执行相同的操作。 我读过 operator.itemget
我有一个 Seq[((元组 A),(元组 B))] 有没有一种简单的方法来对元组 A 进行分组,以便我得到 Seq[(Tuple A, Seq[Tuple B])] 我试过 groupby(x =>
如果我有以下内容 val A = List(1,2,3) val B = List(1,2,3) 这两个变量是否有相同的内存地址? 最佳答案 它们不会有相同的内存地址,可以使用 eq 方法确认,com
我实际上是在尝试创建一个配对列表,但事实证明这非常困难 在有人提到 Hashtables 之前请注意,会有我不关心的重复项。 例如,如果我这样做 $b = @{"dog" = "cat"} 我明白了
我正在尝试为其他资源中的 for_each 循环创建局部变量,但无法按预期制作局部映射。 以下是我试过的。 (地形 0.12) 预期映射到循环 temple_list = { "test2-role"
我目前正在学习 Haskell,在 FP 方面我绝对是初学者。 现在我正在尝试使用列表推导式进行不同的操作。 listComprehension = [(a,b,c) | a <- xs, b <
我正在尝试为其他资源中的 for_each 循环创建局部变量,但无法按预期制作局部映射。 以下是我试过的。 (地形 0.12) 预期映射到循环 temple_list = { "test2-role"
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 9 年前。 Improve th
关闭。这个问题是not reproducible or was caused by typos .它目前不接受答案。 这个问题是由于错别字或无法再重现的问题引起的。虽然类似的问题可能是on-topi
如何通过元组中的第三项过滤此类型的列表: type Car = (String, [String], Int [String]) 我看到了 snd和 fst方法,但在这里我认为这行不通,我不确定如何在
有没有办法创建 Tuple 在 Java 中,无需创建多个类? 例如,可以为每种不同类型的元组创建不同的类,每个类具有不同数量的 Type Parameters : public class Sing
我必须处理一堆二维点类型:pair , pair , pair ,并且只要存在坐标转换,我就允许点之间的隐式转换。像这样: template inline operator pair ( pair t
这个问题在这里已经有了答案: How do I iterate through two lists in parallel? (8 个答案) How do I iterate over the tu
编写一个函数 square_odd_terms 接受一个元组作为参数并返回一个元组中奇数项的平方的元组。即使是条款也将保持不变。 我的尝试是: def square_odd_termms(tpl):
更新: 我选择了这个: set(item[1] for item in id) 谢谢你们,你们的想法对我有帮助。 我正在处理一个元组列表: 以下面这行代码为例。我的 list 可以是任何长度。但是,我
我一直在尝试执行此任务,在尝试时我不禁想到会有比我一直尝试的方式更好的编码方式。 我有一行文字和一个关键字。我想在每个列表中的每个字符下创建一个新列表。关键字将重复自身直到列表末尾。如果有任何非字母字
我现在这个问题已经被问过好几次了。但是,答案似乎并没有解决我的问题。我收到类型错误,“元组”对象不可调用。即使列表中的元组以正确的方式用逗号分隔,我也得到了这个: def aiMove(b):
嘿,所以我花了两个多小时试图解决这个问题,但我就是做不对。我猜我犯了一个非常简单的错误,所以如果有人能指出我正确的方向,我将非常感激,谢谢!顺便说一句,这是一门树屋类(class)。 “目前我们的问候
这不是一个严格的编程问题,但为什么是tuple在单独的 header 中定义,而不是添加到 连同 pair ?它看起来更自然,不那么困惑等。 最佳答案 在具有细粒度的 header 和只有一个 hea
我是一名优秀的程序员,十分优秀!