Python脚本不会像应该的那样将SQL查询的结果写入平面JSON文件
我有一个python脚本,它应该将它应该执行的许多sql查询的结果写入JSON文件。脚本运行正常,但不会将任何结果写入JSON文件。如果我能得到任何帮助,我将不胜感激!我认为错误发生在第59-69行 代码Python脚本不会像应该的那样将SQL查询的结果写入平面JSON文件,python,sql,json,python-3.x,Python,Sql,Json,Python 3.x,我有一个python脚本,它应该将它应该执行的许多sql查询的结果写入JSON文件。脚本运行正常,但不会将任何结果写入JSON文件。如果我能得到任何帮助,我将不胜感激!我认为错误发生在第59-69行 代码 from __future__ import print_function try: import psycopg2 except ImportError: raise ImportError('\n\033[33mpsycopg2 library missing. pip
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()
它能拿回证书吗?(尝试更多的打印语句)
certificate\u store=cursor.fetchall()
此行不应该在数据库的上下文管理器中吗connection@AlbinPaul谢谢你的回复!哪一行是上下文管理器?@bedford line 62带有psycopg2.connect(“dbname=certwatch user=guest host=crt.sh”。format(DB\u NAME,DB\u user,DB\u host))作为conn
是一个上下文管理器必须在证书存储=cursor.fetchall()
中选中该行,然后重试