为什么我的代码不将其从build_info函数收到的数据写入JSON文件?
api = shodan.Shodan(api_key)
query = 'MongoDB Server Information \n{ "process": "mongod" port:27017'
build_info_arr = []
try:
results = api.search(query)
print('Total Results: %s\n' % results['total'])
for result in results['matches']:
if "Authentication partially enabled" not in result['data']:
print('IP: {}'.format(result['ip_str']))
ip: str = format(result['ip_str'])
collections = mongodb_search.build_info(ip)
if collections:
data = json.loads(collections)
for build_infos in data.items():
build_info_arr.append(build_infos)
except shodan.APIError as e:
print('Error: {}'.format(e))
with open('./test.json', "wt") as jsonfile:
jsonfile.write(json.dumps(build_info_arr, indent=4, sort_keys=False))
我的build_info函数在这里:
def build_info(ip):
try:
client = pymongo.MongoClient(ip, 27017, maxPoolSize=10)
data = ''
for build_info in client.db.command({'buildInfo': 1}):
data += str(json.dumps(build_info))
file = json.loads(data)
return file
except:
print('Error: Cannot retrieve buildinfo.')
api = shodan.Shodan(api_key)
query = 'MongoDB Server Information \n{ "process": "mongod" port:27017'
build_info_arr = []
try:
results = api.search(query)
print('Total Results: %s\n' % results['total'])
for result in results['matches']:
if "Authentication partially enabled" not in result['data']:
print('IP: {}'.format(result['ip_str']))
ip: str = format(result['ip_str'])
collections = mongodb_search.build_info(ip)
if collections:
data = json.loads(collections)
for build_infos in data.items():
build_info_arr.append(build_infos)
except shodan.APIError as e:
print('Error: {}'.format(e))
with open('./test.json', "wt") as jsonfile:
jsonfile.write(json.dumps(build_info_arr, indent=4, sort_keys=False))
My build_info function is here:
def build_info(ip):
try:
client = pymongo.MongoClient(ip, 27017, maxPoolSize=10)
data = ''
for build_info in client.db.command({'buildInfo': 1}):
data += str(json.dumps(build_info))
file = json.loads(data)
return file
except:
print('Error: Cannot retrieve buildinfo.')
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
json.dump()
而不是json.dumps()
直接写入文件,请尝试更改以下:You can use
json.dump()
instead ofjson.dumps()
to writes the file directly, try changing to this:与您的问题不是直接相关的,而是几件事:
mongodb.buildinfo
属性中: https://datapedia.shodan.io/property/mongodb.htmlshodan.search_cursor(query)
方法迭代结果。它为您处理分页。请参阅: https://help.shodan.io/guides/guides/guides/如何掉落data-with-apiNot directly related to your question but a few things:
mongodb.buildInfo
property: https://datapedia.shodan.io/property/mongodb.htmlShodan.search_cursor(query)
method to iterate over results. It handles paging for you. See: https://help.shodan.io/guides/how-to-download-data-with-api