在我的python脚本中没有JSON文件的输出
我有一个python脚本,应该执行SQL查询。但是,脚本没有写入JSON文件,当我打开JSON文件时,它是空的。但是,当脚本运行时,我没有得到任何错误。我已尝试修复此错误,但无效。我将非常感谢你的帮助!我添加了certificate\u store=cursor.fetchall(),但即使添加了它似乎也无法修复我的代码 代码在我的python脚本中没有JSON文件的输出,python,sql,python-3.x,database-connection,Python,Sql,Python 3.x,Database Connection,我有一个python脚本,应该执行SQL查询。但是,脚本没有写入JSON文件,当我打开JSON文件时,它是空的。但是,当脚本运行时,我没有得到任何错误。我已尝试修复此错误,但无效。我将非常感谢你的帮助!我添加了certificate\u store=cursor.fetchall(),但即使添加了它似乎也无法修复我的代码 代码 from __future__ import print_function try: import psycopg2 except ImportError:
from __future__ import print_function
try:
import psycopg2
except ImportError:
raise ImportError('\n\033[33mpsycopg2 library missing. pip install psycopg2\033[1;m\n')
sys.exit(1)
import re
import sys
import json
import pprint
import time
import psycopg2.extras
outfilepath = "crtsh_output/crtsh_flat_file"
DB_HOST = 'crt.sh'
DB_NAME = 'certwatch'
DB_USER = 'guest'
DELAY = 0
def connect_to_db():
start = 0
offset = 10
flag = True
while flag:
filepath = 'forager.txt'
print('am i stuck')
with open(filepath, "at+") as fp, open(outfilepath, "wt+") as outfile:
try:
for cnt, domain_name in enumerate(fp):
print("Line {}: {}".format(cnt, domain_name))
print(domain_name)
domain_name = domain_name.rstrip()
SQL = '''
SELECT c.id, x509_commonName(c.certificate) as common_name,
x509_issuerName(c.certificate) as issuer_name,
x509_notBefore(c.certificate) as not_before,
x509_notAfter(c.certificate) as not_after,
x509_keyAlgorithm(c.certificate) as key_algorithm,
x509_keySize(c.certificate) as key_size,
x509_serialNumber(c.certificate) as serial_number,
x509_signatureHashAlgorithm(c.certificate) as signature_hash_algorithm,
x509_signatureKeyAlgorithm(c.certificate) as signature_key_algorithm,
x509_subjectName(c.certificate) as subject_name,
x509_name(c.certificate) as name,
x509_altNames(c.certificate) as alt_names
FROM certificate c, certificate_identity ci
WHERE c.id = ci.certificate_id
AND ci.name_type = 'dNSName'
AND lower(ci.name_value) = lower(%s)
AND x509_notAfter(c.certificate) > statement_timestamp()
'''
with psycopg2.connect("dbname=certwatch user=guest host=crt.sh".format(DB_NAME, DB_USER, DB_HOST)) as conn:
cursor = conn.cursor(cursor_factory=psycopg2.extras.NamedTupleCursor)
cursor.execute(SQL, (domain_name,))
certificate_store = cursor.fetchall()
for certificate in certificate_store:
print(certificate.id)
print(certificate.common_name)
print(certificate.issuer_name)
outfile.write(json.dumps(certificate, sort_keys=True, indent=4, default=str, ensure_ascii=False))
# query db with start and offset
# unique_domains = cursor.fetchall()
# if not unique_domains:
# flag = False
# else:
# do processing with your data
# pprint.pprint(unique_domains)
# outfile.write(json.dumps(unique_domains, sort_keys=True, indent=4, default=str, ensure_ascii = False))
# offset += limit
except Exception as error:
print(str(error))
if __name__ == "__main__":
connect_to_db()
写入文件的行:
outfile.write(...)
受到<代码>的保护,如果<代码>条件:
if not unique_domains:
flag = False
else:
....
outfile.write(...)
with psycopg2.connect("dbname=certwatch user=guest host=crt.sh".format(DB_NAME, DB_USER, DB_HOST)) as conn:
因此,unique_域
必须是True
ish
但是你的台词呢
unique_domains = cursor.fetchall()
不在数据库的块内,条件为:
if not unique_domains:
flag = False
else:
....
outfile.write(...)
with psycopg2.connect("dbname=certwatch user=guest host=crt.sh".format(DB_NAME, DB_USER, DB_HOST)) as conn:
此外,您已经获取(并在打印后丢弃)中的记录
for certificate in cursor.fetchall():
因此,除非再次执行查询,否则无法再次获取它们。更好的办法是在第一次提取时存储数据。您从光标提取了两次数据。另外,请为您的文件使用全局路径名。请尝试关闭文件流。我认为丢失的outfile.close可能是导致问题的原因。谢谢您的帖子!我在上面发布了我的新代码,显示我在代码中添加了certificate\u store=cursor.fetchall()。是这样吗?如果没有,我将非常感谢你的帮助@bedford您仍然多次调用fetchall()
-您应该执行certificate\u store=cursor.fetchall()
,然后对certificate\u store中的证书使用。。。您不能再调用fetchall()
。谢谢您的回复!我做了这些更改,并更新了上面的代码。我在代码中添加了certificate\u store=cursor.fetchall()作为certificate\u store:中的证书,但它仍然不起作用。我将非常感谢你的帮助@bedford现在您只调用了一次fetchall
,这似乎很好。但是数据库代码仍然在数据库连接的with
块之外。这意味着数据库在运行时是关闭的。我发布了更新后的代码,数据库代码缩进,但仍然不起作用。