有没有使用 python 打印客户端 ip 地址?

Is there anyway to print clients ip address using python?

db.currentOp(true).inprog.forEach(function(d){if(d.client)print(d.client, d.connectionId)})

输出:

127.0.0.1:64629 383
127.0.0.1:56878 363

我使用此命令获取 shell 中客户端的 IP 地址。 有什么方法可以在 python 中获取它吗?

db.currentOpmanual page

db.currentOp wraps the database command currentOp.

所以一旦我们知道了这一点,我们就可以在 pymongo 中使用 db.command(),并按如下方式复制您的 javascript:

from pymongo import MongoClient

db = MongoClient()['admin']
command_result = db.command({'currentOp': 1})
inprogs = command_result.get('inprog')

for inprog in inprogs:
    if 'client' in inprog:
        print(inprog.get('client'), inprog.get('connectionId'))

给出(例如):

172.17.0.1:39734 15