ssh隧道连接mongo

#!/usr/bin/python3
import sys,os
from requests.api import get
from sshtunnel import SSHTunnelForwarder
from pymongo import MongoClient, DESCENDING, ASCENDING, errors
sys.path.append(os.path.join(os.getcwd()))
import readConfig


localReadConfig = readConfig.ReadConfig()


class MdbOpera(object):
    def __init__(self,db="USERDB"):
        self.server = self.get_server()
        self.server.start()
        self.client = MongoClient(
            host='127.0.0.1',
            port= self.server.local_bind_port,
            username=localReadConfig.get_mongo("username"),
            password=localReadConfig.get_mongo("password"),
            maxIdleTimeMS=30 * 1000,
            maxpoolsize=100
        )
        self.db = self.client[db]

    def get_server(self):
        ssh_address = localReadConfig.get_ssh("host") ##服务器地址
        ssh_port = int(localReadConfig.get_ssh("port"))
        server_user = localReadConfig.get_ssh("username") ##登录服务器的用户
        server_password = localReadConfig.get_ssh("password") ##登录服务器的密码
        server = SSHTunnelForwarder(
            ssh_address_or_host = (ssh_address, ssh_port),
            ssh_password=server_password,
            ssh_username=server_user,
            remote_bind_address=(localReadConfig.get_mongo("host"),int(localReadConfig.get_mongo("port"))),
            local_bind_address=("0.0.0.0", 9527) #绑定到本地的端口
            )
        return server  

    def __del__(self):
        self.server.stop()

    def reconnect(self, db="test"):  # MongoClient 数据库  MongoClient[xxx]  表名
        if self.client:
            self.db = self.client[db]
            return 1

    # 指定表的增删改查
    def insertone(self, data, collection="NetWork"):
        col = self.db[collection]
        try:
            InsertOneResult = col.insert_one(data)
            if InsertOneResult.inserted_id:
                return 1
            else:
                return -1
        except Exception as e:
            print(e)
            return False

    def insertmany(self, data, collection="NetWork"):
        col = self.db[collection]
        try:
            result = col.insert_many(data)
            return result
        except Exception as e:
            print(e)
            return False

    def deleteone(self, data, collection="NetWork"):
        col = self.db[collection]
        try:
            result = col.delete_one(data)
            return result
        except Exception as e:
            print(e)
            return False

    def remove(self, data, collection="NetWork"):
        col = self.db[collection]
        try:
            return col.remove(data)
        except Exception as e:
            print(e)
            return False

    def findall_count(self, rule={}, collection="NetWork"):
        """
        统计总数
        """
        col = self.db[collection]
        return col.find(rule).count()

    def findall(self, rule={}, limitrange=[0, 0], collection="NetWork"):
        col = self.db[collection]
        summary = col.count()
        if limitrange == [0, 0]:
            addrobj = col.find(rule)
        else:
            addrobj = col.find(rule).skip(limitrange[0]).limit(limitrange[1] - limitrange[0])
        return addrobj, addrobj.count()  # 返回搜索结果数量 不算limit限制的

    def update_insert(self, olddata, newdata, collection="NetWork"):
        try:
            col = self.db[collection]
            return col.update(olddata, newdata, upsert=True)
        except Exception as err:
            return False

    def update(self, olddata, newdata, collection="NetWork"):
        try:
            col = self.db[collection]
            return col.update(olddata, newdata)
        except Exception as err:
            return False

    def findone(self, data, collection="NetWork"):
        try:
            col = self.db[collection]
            return col.find_one(data)
        except Exception as err:
            return False

    def findall_sort(self, rule={}, sortitem="", sortparam=1, limitrange=[0, 0], collection="NetWork", projection=None):
        col = self.db[collection]
        if sortparam:
            sortparam = ASCENDING
        else:
            sortparam = DESCENDING
        if limitrange == [0, 0]:
            if projection:
                addrobj = col.find(rule, projection).sort(sortitem, sortparam)
            else:
                addrobj = col.find(rule).sort(sortitem, sortparam)
        else:
            if projection:
                addrobj = col.find(rule, projection).sort(sortitem, sortparam).skip(limitrange[0]).limit(
                    limitrange[1] - limitrange[0])
            else:
                addrobj = col.find(rule).sort(sortitem, sortparam).skip(limitrange[0]).limit(
                    limitrange[1] - limitrange[0])
        return addrobj, addrobj.count()  # 慢就一个字

    def findone_sort(self, data, sortitem, sortparam, collection="NetWork"):
        # print data
        col = self.db[collection]
        if sortparam:
            sortparam = ASCENDING
        else:
            sortparam = DESCENDING
        cursor = col.find(data).sort(sortitem, sortparam)
        if cursor.count() > 0:
            return cursor[0]
        else:
            return False

    def update_one(self, data, modifydata, collection="NetWork"):
        try:
            col = self.db[collection]
            return col.update_one(data, modifydata)
        except Exception as err:
            return False

    def update_many(self, data, modifydata, collection="NetWork"):
        try:
            col = self.db[collection]
            return col.update_many(data, modifydata)
        except Exception as err:
            return False

    def easy_distinct(self, key, collection="NetWork"):
        """
        简单去重,返回值是查询的关键字的list
        """
        col = self.db[collection]
        return col.distinct(key)

    # 返回取到的list 与总list长度
    # distinct 消重
    def distinct_find(self, key, data={}, limitrange=[0, 0], collection="NetWork"):
        col = self.db[collection]
        list_dis = col.find(data).distinct(key)
        len_list_dit = len(list_dis)
        if limitrange == [0, 0]:
            return list_dis, len_list_dit
        elif limitrange[1] > len_list_dit:
            return list_dis, len_list_dit
        else:
            return list_dis[limitrange[0]:limitrange[1]], len_list_dit

    # 全col计数group
    def aggregate_total(self, data, collection="NetWork"):
        col = self.db[collection]
        result = col.aggregate([{"$group": {"_id": data, "count": {"$sum": 1}}}])
        return list(result)

    # 条件计数group
    def aggregate_match_total(self, match, data, collection="NetWork"):
        col = self.db[collection]
        result = col.aggregate([{"$match": match}, {"$group": {"_id": data, "count": {"$sum": 1}}}])
        # result = col.aggregate([{"$group":{"_id":data,"count":{"$sum":1}}}])
        return list(result)

    # 条件计数group,获取最大最小值
    def aggregate_match_total_max_min(self, match, data, max_minitem, collection="NetWork"):
        col = self.db[collection]
        result = col.aggregate([{"$match": match}, {"$group": {"_id": data, "min": {"$min": max_minitem},
                                                               "max": {"$max": max_minitem}, "count": {"$sum": 1}}}],
                               allowDiskUse=True)
        # result = col.aggregate([{"$group":{"_id":data,"count":{"$sum":1}}}])
        return list(result)  # ,"buf": {"$addToSet": "$info" }

    # aggregate 自定义输入
    def AggregatePipeLine(self, data, collection=""):
        col = self.db[collection]
        result = col.aggregate(data, allowDiskUse=True)  # [{"$group":{"_id":data,"count":{"$sum":1}}}]
        return list(result)

    def delete_many(self, data, collection="NetWork"):
        col = self.db[collection]
        return col.delete_many(data)

    def addindex(self):
        col = self.db["xxx"]
        col.create_index([("md5", -1)])
        return 1

    def findmax(self, rule, sort_field, collection="test"):
        col = col = self.db[collection]
        res = col.find(rule).sort([(sort_field, -1)]).limit(1)
        try:
            data = next(res)
        except StopIteration as error:
            return None
        return data

    def findmin(self, rule, sort_field, collection="test"):
        col = self.db[collection]
        res = col.find(rule).sort([(sort_field, 1)]).limit(1)
        try:
            data = next(res)
        except StopIteration as error:
            return None
        return data

    def expire_time(self,timeout=3600,collection="test"):
        try:
            col = self.db[collection]
            res = col.create_index([("expire_time", ASCENDING)],expireAfterSeconds= timeout)
            return res
        except Exception as err:
            return False

    def create_capped_collection(self,collection="test",cap_size=1000*1024*1024,max_size=10000):
        try:
            try:
                if collection not in self.db.list_collection_names():
                    self.db.create_collection(name=collection, capped=True, size=cap_size, max=max_size)
                else:
                    col = self.db[collection]
                    message = col.options()
                    if not message.get("capped", ""):
                        col.drop()
                        self.db.create_collection(name=collection, capped=True, size=cap_size, max=max_size)
                return True
            except errors.ServerSelectionTimeoutError as err:
                print(err)
                print("退出 uwsgi")
                sys.exit()
        except Exception as err:
            print('异常')
            print(err)
            return False

if __name__ == "__main__":
    mongodb_user = MdbOpera(db="xxx")
    print(mongodb_user)
    match_rule = {"ip": "xxx"}
    find_data = mongodb_user.findone(data=match_rule, collection="xxx")
    if not find_data:
        print(False)
    print(find_data.get("payload"))
    # print(mongodb_user.db.find())
    del mongodb_user


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值