将MongoDB数组类型值分配给Python变量

时间:2018-01-21 21:36:54

标签: python mongodb pymongo

我正在尝试使用PyMongo将Login和Password JSON参数分配给python变量。这是存储在Mongo中的JSON:

{"_id":"5a2700c9eb0a197ba6f8120d"
"protocol":"ssh"
"hpfeed_id":"5a2700c7eb0a197ba6f8120a"
"timestamp":"2017-12-05T20:25:43.922Z"
"source_ip":"37.221.198.242"
"session_ssh":{"version":"SSH-2.0-libssh2_1.4.3"}
"source_port":45557
"destination_port":22
"identifier":"da258962-c5b5-11e7-9c0a-1e7dbf5015ae"
"honeypot":"cowrie"
"auth_attempts":[{"login":"worker2","password":"worker2"}]}

enter image description here

auth_attempts是一种数组类型,我不能在我的生活中弄清楚如何将它变成Python中的字符串。这是我到目前为止所尝试的内容:

# Query the MongoDB for required data
def query():
    sessionData = session.find({})
    numOfSessions = session.find({}).count()
    pprint.pprint(session.find_one({'auth_attempts': { 'login': [0] }}))
    for item in sessionData:
       if (item['honeypot'] != 'p0f'):
            test = item({'auth_attempts': { '$in': [0] }})
            print(str(test))

1 个答案:

答案 0 :(得分:1)

我猜你想要一个字符串:

['my_script.py', 'hello', 'world']

从您的代码段中,您的项目是一个字典。因此,要获得字符串中的auth_attempts,您应该可以执行以下操作:

"login: worker2, password:worker2"