当前位置: 首页>>代码示例>>Python>>正文


Python connection.Connection类代码示例

本文整理汇总了Python中pymongo.connection.Connection的典型用法代码示例。如果您正苦于以下问题:Python Connection类的具体用法?Python Connection怎么用?Python Connection使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了Connection类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_document_class

    def test_document_class(self):
        c = Connection(self.host, self.port)
        db = c.pymongo_test
        db.test.insert({"x": 1})

        self.assertEqual(dict, c.document_class)
        self.assertTrue(isinstance(db.test.find_one(), dict))
        self.assertFalse(isinstance(db.test.find_one(), SON))

        c.document_class = SON

        self.assertEqual(SON, c.document_class)
        self.assertTrue(isinstance(db.test.find_one(), SON))
        self.assertFalse(isinstance(db.test.find_one(as_class=dict), SON))

        c = Connection(self.host, self.port, document_class=SON)
        db = c.pymongo_test

        self.assertEqual(SON, c.document_class)
        self.assertTrue(isinstance(db.test.find_one(), SON))
        self.assertFalse(isinstance(db.test.find_one(as_class=dict), SON))

        c.document_class = dict

        self.assertEqual(dict, c.document_class)
        self.assertTrue(isinstance(db.test.find_one(), dict))
        self.assertFalse(isinstance(db.test.find_one(), SON))
开发者ID:cathoderay,项目名称:mongo-python-driver,代码行数:27,代码来源:test_connection.py

示例2: _open

    def _open(self):
        conninfo = self.connection.client
        mongoconn = Connection(host=conninfo.hostname, port=conninfo.port)
        dbname = conninfo.virtual_host
        version = mongoconn.server_info()["version"]
        if tuple(map(int, version.split(".")[:2])) < (1, 3):
            raise NotImplementedError(
                "Kombu requires MongoDB version 1.3+, but connected to %s" % (
                    version, ))
        if not dbname or dbname == "/":
            dbname = "kombu_default"
        database = getattr(mongoconn, dbname)
        if conninfo.userid:
            database.authenticate(conninfo.userid, conninfo.password)

        self.db = database
        col = database.messages
        col.ensure_index([("queue", 1)])

        if "messages.broadcast" not in database.collection_names():
            capsize = conninfo.transport_options.get(
                            "capped_queue_size") or 100000
            database.create_collection("messages.broadcast", size=capsize,
                                                             capped=True)

        self.bcast = getattr(database, "messages.broadcast")
        self.bcast.ensure_index([("queue", 1)])

        self.routing = getattr(database, "messages.routing")
        self.routing.ensure_index([("queue", 1), ("exchange", 1)])
        return database
开发者ID:Kronuz,项目名称:kombu,代码行数:31,代码来源:mongodb.py

示例3: TestConfig

class TestConfig(unittest.TestCase):

    def setUp(self):
        filename = dirname(__file__) + '/logging-test.config'
        fileConfig(filename)

        """ Create an empty database that could be used for logging """
        self.db_name = '_mongolog_test'
        self.collection_name = 'log_test'

        self.conn = Connection('localhost')
        self.conn.drop_database(self.db_name)

    def tearDown(self):
        """ Drop used database """
        self.conn.drop_database(self.db_name)

    def testLoggingFileConfiguration(self):
        log = logging.getLogger('example')
        log.debug('test')

        r = self.conn[self.db_name][self.collection_name]

        message = r.find_one({'level': 'debug', 'msg': 'test'})
        self.assertEquals(message['msg'], 'test')
开发者ID:natuangloops,项目名称:mongodb-log,代码行数:25,代码来源:test_config.py

示例4: test_connection

    def test_connection(self):
        c = Connection(host, port)
        self.assertTrue(c.auto_start_request)
        self.assertEqual(None, c.max_pool_size)
        self.assertFalse(c.slave_okay)
        self.assertFalse(c.safe)
        self.assertEqual({}, c.get_lasterror_options())

        # Connection's writes are unacknowledged by default
        doc = {"_id": ObjectId()}
        coll = c.pymongo_test.write_concern_test
        coll.drop()
        coll.insert(doc)
        coll.insert(doc)

        c = Connection("mongodb://%s:%s/?safe=true" % (host, port))
        self.assertTrue(c.safe)

        # To preserve legacy Connection's behavior, max_size should be None.
        # Pool should handle this without error.
        self.assertEqual(None, get_pool(c).max_size)
        c.end_request()

        # Connection's network_timeout argument is translated into
        # socketTimeoutMS
        self.assertEqual(123, Connection(
            host, port, network_timeout=123)._MongoClient__net_timeout)

        for network_timeout in 'foo', 0, -1:
            self.assertRaises(
                ConfigurationError,
                Connection, host, port, network_timeout=network_timeout)
开发者ID:Bloodevil,项目名称:mongo-python-driver,代码行数:32,代码来源:test_legacy_connections.py

示例5: test_from_uri

    def test_from_uri(self):
        c = Connection(self.host, self.port)

        self.assertRaises(InvalidURI, Connection.from_uri, "mongodb://localhost/baz")

        self.assertEqual(c, Connection.from_uri("mongodb://%s:%s" %
                                                (self.host, self.port)))

        c.admin.system.users.remove({})
        c.pymongo_test.system.users.remove({})

        c.admin.add_user("admin", "pass")
        c.pymongo_test.add_user("user", "pass")

        self.assertRaises(InvalidURI, Connection.from_uri,
                          "mongodb://foo:[email protected]%s:%s" % (self.host, self.port))
        self.assertRaises(InvalidURI, Connection.from_uri,
                          "mongodb://admin:[email protected]%s:%s" % (self.host, self.port))
        self.assertRaises(InvalidURI, Connection.from_uri,
                          "mongodb://user:[email protected]%s:%s" % (self.host, self.port))
        Connection.from_uri("mongodb://admin:[email protected]%s:%s" % (self.host, self.port))

        self.assertRaises(InvalidURI, Connection.from_uri,
                          "mongodb://admin:[email protected]%s:%s/pymongo_test" %
                          (self.host, self.port))
        self.assertRaises(InvalidURI, Connection.from_uri,
                          "mongodb://user:[email protected]%s:%s/pymongo_test" %
                          (self.host, self.port))
        Connection.from_uri("mongodb://user:[email protected]%s:%s/pymongo_test" %
                            (self.host, self.port))
开发者ID:mcfunley,项目名称:mongo-python-driver,代码行数:30,代码来源:test_connection.py

示例6: __init__

class plantmongo:
    def __init__(self, addr, user="plant", password="plant"):
        self.addr = addr
        self.user = user
        self.password = password
        self.conn = Connection(addr)
        self.db = conn.plant
        if not db.authenticate("plant", "plant"):
            print "unable to authenticate to mongodb"
            self.connected = False
            return None
        self.coll = self.db.test

    def publish(self, xbeeobj):
        post = {
            "time": time.time(),
            "temp": xbeeobj.temp,
            "moisture": xbeeobj.moisture,
            "dewpoint": xbeeobj.dewpoint,
            "pressure": xbeeobj.pressure,
            "light": xbeeobj.light,
            "batt": xbeeobj.batt,
            "rssi": xbeeobj.rssi,
        }
        self.coll.save(post)

    def close(self):
        self.conn.disconnect()
开发者ID:theterg,项目名称:plantXBee,代码行数:28,代码来源:plantmongo.py

示例7: MongoHandler

class MongoHandler(logging.Handler):
    """ Custom log handler

    Logs all messages to a mongo collection. This  handler is 
    designed to be used with the standard python logging mechanism.
    """

    @classmethod
    def to(cls, db, collection, host='localhost', port=None, level=logging.NOTSET):
        """ Create a handler for a given  """
        return cls(Connection(host, port)[db][collection], level)
        
    def __init__(self, collection, db='mongolog', host='localhost', port=None, level=logging.NOTSET):
        """ Init log handler and store the collection handle """
        logging.Handler.__init__(self, level)
        if (type(collection) == str):
            self.collection = Connection(host, port)[db][collection]
        else:
            self.collection = collection
        self.formatter = MongoFormatter()

    def emit(self,record):
        """ Store the record to the collection. Async insert """
        try:
            self.collection.save(self.format(record))
        except InvalidDocument, e:
            logging.error("Unable to save log record: %s", e.message ,exc_info=True)
开发者ID:achun2080,项目名称:mongodb-log,代码行数:27,代码来源:handlers.py

示例8: WriteTempFile

    def WriteTempFile(self, data, hash_name=None):
        
        if self.use_cache == True:
            if hash_name is None:
                hash = md5(self.url )
                hash_name = hash.hexdigest()
                self.last_hash_name = hash_name
                
            self.log.debug('write file to cache: ', hash_name)
            self.log.debug('use mongo: %s' % self.use_mongo)
#            open(self.download_temp+hash_name, 'wb').write(data)
            if self.use_mongo == False: 
                f_name = self.download_temp + hash_name + '.gz'
                f = gzip.open(f_name, 'wb')
                f.write(data)
                f.close()

            if self.use_mongo == True:
                connection = Connection("localhost", 27017)
                db = connection['parser']

                s = StringIO.StringIO()
                f = gzip.GzipFile(fileobj=s, mode='wb')
                f.write(data)
                f.close()
                val = s.getvalue()
                s.close()
                del (s)
                del (f)

                fs = GridFS(db)
                fp = fs.open(hash_name , 'w', self.download_temp.replace('/', '') )
                fp.write(val)
                fp.close()
                connection.disconnect()
开发者ID:darkman66,项目名称:PageGet,代码行数:35,代码来源:Requester.py

示例9: test_unix_domain_socket

 def test_unix_domain_socket(self):
     try:
         connection = Connection("/tmp/mongodb-27017.sock")
         connection.database_names()
     except:
         # Possibly the OS doesn't support unix domain sockets
         raise SkipTest("No Unix domain sockets")
开发者ID:NunoEdgarGub1,项目名称:mongo-python-driver,代码行数:7,代码来源:test_connection.py

示例10: test_connection

    def test_connection(self):
        c = Connection(host, port)
        self.assertTrue(c.auto_start_request)
        self.assertFalse(c.slave_okay)
        self.assertFalse(c.safe)
        self.assertEqual({}, c.get_lasterror_options())

        # Connection's writes are unacknowledged by default
        doc = {"_id": ObjectId()}
        coll = c.pymongo_test.write_concern_test
        coll.really_drop()
        coll.insert(doc)
        coll.insert(doc)

        c = Connection("mongodb://%s:%s/?safe=true" % (host, port))
        self.assertTrue(c.safe)

        # Connection's network_timeout argument is translated into
        # socketTimeoutMS
        self.assertEqual(123, Connection(
            host, port, network_timeout=123)._MongoClient__net_timeout)

        for network_timeout in 'foo', 0, -1:
            self.assertRaises(
                ConfigurationError,
                Connection, host, port, network_timeout=network_timeout)
开发者ID:conversocial,项目名称:mongo-python-driver,代码行数:26,代码来源:test_legacy_connections.py

示例11: __init__

 def __init__(self):
     self.__cfgReporting = Config(os.path.join(RAGPICKER_ROOT, 'config', 'reporting.conf'))
     self.__cfgProcessing = Config(os.path.join(RAGPICKER_ROOT, 'config', 'processing.conf'))
     self.__mongodbEnabled = self.__cfgReporting.getOption("mongodb", "enabled")
     self.__codedbEnabled = self.__cfgReporting.getOption("codeDB", "enabled")
     self.__bluecoatEnabled = self.__cfgProcessing.getOption("all_bluecoatMalwareAnalysisAppliance", "enabled")
     
     if self.__mongodbEnabled:
         #Anbindung an Datenbank MongoDB Collection Ragpicker herstellen
         try:
             mongodbHost = self.__cfgReporting.getOption("mongodb", "host")
             mongodbPort = self.__cfgReporting.getOption("mongodb", "port")
             self.__mongodbConnection = Connection(mongodbHost, mongodbPort)
             self.__mongodbCollectionRagpicker = self.__mongodbConnection.MalwareAnalyse.ragpicker
             self.__mongodbCollectionFamilies = self.__mongodbConnection.MalwareAnalyse.families
             self.__mongodbCollectionSandboxTaskQueue = self.__mongodbConnection.MalwareAnalyse.sandboxTaskQueue
         except TypeError:
             raise Exception("MongoDB connection port in report.config must be integer")
         except ConnectionFailure:
             raise Exception("Cannot connect to MongoDB (ragpicker)")
     
     if self.__codedbEnabled:
         #Anbindung an Datenbank MongoDB Collection CodeDB herstellen
         try:
             codedbHost = self.__cfgReporting.getOption("codeDB", "mongo_db_host")
             codedbPort = self.__cfgReporting.getOption("codeDB", "mongo_db_port")
             self.__codedbConnection = Connection(codedbHost, codedbPort)
             self.__codedbCollectionCodedb = self.__codedbConnection.MalwareAnalyse.codeDB
         except TypeError:
             raise Exception("MongoDB connection port for CodeDB in report.config must be integer")
         except ConnectionFailure:
             raise Exception("Cannot connect to MongoDB (codeDB)")  
开发者ID:Beercow,项目名称:malware-crawler,代码行数:32,代码来源:database.py

示例12: test_ipv6

    def test_ipv6(self):
        self.assertRaises(InvalidURI, _parse_uri, "::1", 27017)
        self.assertRaises(InvalidURI, _parse_uri, "[::1", 27017)
        self.assertRaises(InvalidURI, _parse_uri, "::1]:27017")
        self.assertRaises(InvalidURI, _parse_uri, "mongodb://::1", 27017)
        self.assert_(_parse_uri, "mongodb://[::1]:27017/?slaveOk=true")
        self.assert_(_parse_uri,
                     "[::1]:27017,[2001:0db8:85a3:0000:0000:8a2e:0370:7334]"
                     ":27018,192.168.0.212:27019,localhost:27020")
        self.assert_(_parse_uri,
                     "mongodb://[2001:0db8:85a3:0000:0000:8a2e:0370:7334]"
                     ":27017/?slaveOk=true")
        try:
            connection = Connection("[::1]")
        except:
            # Either mongod was started without --ipv6
            # or the OS doesn't support it (or both).
            raise SkipTest()

        # Try a few simple things
        connection = Connection("mongodb://[::1]:27017")
        connection = Connection("mongodb://[::1]:27017/?slaveOk=true")
        connection = Connection("[::1]:27017,localhost:27017")
        connection = Connection("localhost:27017,[::1]:27017")
        connection.pymongo_test.test.save({"dummy": u"object"})
        connection.pymongo_test_bernie.test.save({"dummy": u"object"})

        dbs = connection.database_names()
        self.assert_("pymongo_test" in dbs)
        self.assert_("pymongo_test_bernie" in dbs)
开发者ID:pmljm,项目名称:mongo-python-driver,代码行数:30,代码来源:test_connection.py

示例13: test_autoreconnect

 def test_autoreconnect(self):
     def find_one(conn):
         return conn.test.stuff.find_one()
     # Simulate a temporary connection failure
     c = Connection('foo', _connect=False)
     self.assertRaises(AutoReconnect, find_one, c)
     c._Connection__nodes = set([('localhost', 27017)])
     self.assert_(find_one, c)
开发者ID:abhayv,项目名称:mongo-python-driver,代码行数:8,代码来源:test_connection.py

示例14: test_database_names

    def test_database_names(self):
        connection = Connection(self.host, self.port)

        connection.pymongo_test.test.save({"dummy": u"object"})
        connection.pymongo_test_mike.test.save({"dummy": u"object"})

        dbs = connection.database_names()
        self.assertTrue("pymongo_test" in dbs)
        self.assertTrue("pymongo_test_mike" in dbs)
开发者ID:cathoderay,项目名称:mongo-python-driver,代码行数:9,代码来源:test_connection.py

示例15: test_end_request

    def test_end_request(self):
        self.skip()
        connection = Connection([self.left, self.right])
        db = connection.pymongo_test

        for _ in range(100):
            db.test.remove({})
            db.test.insert({})
            self.assertTrue(db.test.find_one())
            connection.end_request()
开发者ID:cathoderay,项目名称:mongo-python-driver,代码行数:10,代码来源:test_paired.py


注:本文中的pymongo.connection.Connection类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。