def test_lazyConnectionPool(self): db = txredisapi.lazyConnectionPool(REDIS_HOST, REDIS_PORT, reconnect=False) yield self._wait_for_lazy_connection(db) yield self._assert_simple_sets_on_pipeline(db=db) yield db.disconnect()
def init(): redis_pool = redis.lazyConnectionPool(host='127.0.0.1', port=7979, dbid=0, poolsize=10) print 'before redis pool' con = yield redis_pool._connected print redis_pool gdata["redis_conn"] = redis_pool
def __init__(self, crawler, redis_url): self.redis_url = redis_url self.report_connection_error = True args = RedisPipeline.parse_redis_url(redis_url) self.connection = txredisapi.lazyConnectionPool(connectTimeout=5, replyTimeout=5, **args)
def Init(cls): def helper(conn_obj): conn_obj.execute("BEGIN") models.ZbModule.Init(conn_obj) models.ZbDevice.Init(conn_obj) cls.db_pool = rg_lib.Sqlite.MakeConnPool(settings.BIZ_DB['path']) cls.redis_conn = txredisapi.lazyConnectionPool(host=settings.REDIS['host'], port=settings.REDIS['port'], charset=None, convertNumbers=False) return cls.db_pool.runWithConnection(helper)
def create_application(): rd = txredisapi.lazyConnectionPool() redisBackend = RedisResolverBackend(rd, servers=['8.8.8.8']) application = service.Application("txdnsredis") srv_collection = service.IServiceCollection(application) dnsFactory = server.DNSServerFactory(caches=[cache.CacheResolver()], clients=[redisBackend]) internet.TCPServer(53, dnsFactory).setServiceParent(srv_collection) internet.UDPServer(53, dns.DNSDatagramProtocol(dnsFactory)).setServiceParent(srv_collection) return application
def startService(self): print("startService()") self.sdb = SessionDB(redis=txredisapi.lazyConnectionPool(), session_timeout=self.config.session_timeout, removed_timeout=self.config.removed_timeout) self.cleaner = SessionCleaner(session_db=self.sdb) self._port = reactor.listenUDP( 1813, RADIUSAccountingProtocol( session_db=self.sdb, hosts=self.hosts, rad_dict=dictionary.Dictionary('dictionary')))
def create_application(): rd = txredisapi.lazyConnectionPool(host="127.0.0.1", port=36379, dbid=0, password=None) redisBackend = RedisResolverBackend(rd, servers=[('8.8.8.8', 53)]) application = service.Application("txdnsredis") srv_collection = service.IServiceCollection(application) dnsFactory = server.DNSServerFactory(caches=[cache.CacheResolver()], clients=[redisBackend]) internet.TCPServer(53, dnsFactory).setServiceParent(srv_collection) internet.UDPServer(53, dns.DNSDatagramProtocol(dnsFactory)).setServiceParent(srv_collection) return application
def __init__(self, config=None): ApplicationSession.__init__(self, config) if self.config.extra['redis']: # parsed = urlparse(self.config.extra['redis']) parsed = urlparse(os.getenv('REDIS_PORT_6379_TCP') + '/0') host = parsed.hostname port = parsed.port db = int(parsed.path.replace('/', '')) self.config.extra['redis'] = txredis.lazyConnectionPool( host=host, port=port, dbid=db) self.user_hisory = {} print("component created")
def __init__(self, crawler, redis_url, redis_nm): """Store configuration, open connection and register callback""" # Store the url and the namespace for future reference self.redis_url = redis_url self.redis_nm = redis_nm # Report connection error only once self.report_connection_error = True # Parse redis URL and try to initialize a connection args = RedisCache.parse_redis_url(redis_url) self.connection = txredisapi.lazyConnectionPool(connectTimeout=5, replyTimeout=5, **args) # Connect the item_scraped signal crawler.signals.connect(self.item_scraped, signal=signals.item_scraped)
def makeService(self, config): if config['password'] is None: raise usage.UsageError, "--password is required." if string.find(config['password'], 'env:') == 0: env = string.replace(config['password'], 'env:', '', 1) pwd = os.getenv(env) if pwd is None: raise usage.UsageError, "invalid environment variable in --password option" else: config['password'] = pwd db = redis.lazyConnectionPool(config['redis-host'], config['redis-port'], poolsize=config['redis-pool'], dbid=config['redis-db']) passHolderService = PassHolderService(config['password'], config['scrypt-enctime'], db) return internet.SSLServer(config['port'], IPassHolderFactory(passHolderService), ServerContextFactory(config['server-cert'], config['server-key'], config['ca-cert']), interface=config["listen"] )
def makeService(self, options): config = ConfigParser() config.read(options['config']) dns_port = int(config.get('dns', 'port') or options['port']) redis_client = txredisapi.lazyConnectionPool( host=config.get('redis', 'host'), port=int(config.get('redis', 'port')), dbid=int(config.get('redis', 'db'))) dns_factory = DDNSFactory(redis_client) application = service.Application('ddns') service_collection = service.IServiceCollection(application) internet.TCPServer(dns_port, dns_factory).setServiceParent(service_collection) internet.UDPServer(dns_port, dns.DNSDatagramProtocol( dns_factory)).setServiceParent(service_collection) return service_collection
def makeService(self, config): if config['password'] is None: raise usage.UsageError, "--password is required." if string.find(config['password'], 'env:') == 0: env = string.replace(config['password'], 'env:', '', 1) pwd = os.getenv(env) if pwd is None: raise usage.UsageError, "invalid environment variable in --password option" else: config['password'] = pwd db = redis.lazyConnectionPool(config['redis-host'], config['redis-port'], poolsize=config['redis-pool'], dbid=config['redis-db']) passHolderService = PassHolderService(config['password'], config['scrypt-enctime'], db) return internet.SSLServer(config['port'], IPassHolderFactory(passHolderService), ServerContextFactory(config['server-cert'], config['server-key'], config['ca-cert']), interface=config["listen"])
import json #logging from twisted.python import log as the_log from twisted.python.logfile import DailyLogFile the_log.startLogging(open("./twisted_test.log", 'w')) def log(msg): #shortcut the_log.msg("----- TEST TASK ---->>> " + msg) #mysql with a pool from twisted.enterprise import adbapi mysql_pool = adbapi.ConnectionPool("MySQLdb", db="twisted_test", user="******", passwd="hydrason") #redis import txredisapi as redis redis_pool = redis.lazyConnectionPool() #not Deferred class HttpFetcher(object): """ URL fetching small wrapper """ HTTP_POOL = HTTPConnectionPool(reactor) HTTP_AGENT = Agent(reactor, pool=HTTP_POOL) def __init__(self, method): self.method = method def get(self, url, method=None): return self.HTTP_AGENT.request(method or self.method, url)
def connect(self): self.pool = redis.lazyConnectionPool(host="localhost",port=6379,poolsize=5,reconnect=True)
def test_lazyConnectionPool(self): db = txredisapi.lazyConnectionPool(redis_host, redis_port, reconnect=False) yield self._wait_for_lazy_connection(db) yield self._assert_simple_sets_on_pipeline(db=db) yield db.disconnect()
def log(msg): #shortcut the_log.msg("----- TEST TASK ---->>> " + msg) #mysql with a pool from twisted.enterprise import adbapi mysql_pool = adbapi.ConnectionPool("MySQLdb", db="twisted_test", user="******", passwd="hydrason") #redis import txredisapi as redis redis_pool = redis.lazyConnectionPool() #not Deferred class HttpFetcher(object): """ URL fetching small wrapper """ HTTP_POOL = HTTPConnectionPool(reactor) HTTP_AGENT = Agent(reactor, pool=HTTP_POOL) def __init__(self, method): self.method = method def get(self, url, method=None): return self.HTTP_AGENT.request(method or self.method, url)
def test_lazyConnectionPool(self): db = redis.lazyConnectionPool(redis_host, redis_port, reconnect=False) self.assertEqual(isinstance(db._connected, defer.Deferred), True) db = yield db._connected self.assertEqual(isinstance(db, redis.ConnectionHandler), True) yield db.disconnect()
if len(sys.argv) > 1: if sys.argv[1] == 'debug': log.startLogging(sys.stdout) debug = True elif sys.argv[1].startswith('wsport'): wsportplitted = sys.argv[1].split('=') if len(wsportplitted) > 1: WEBSERVICE_LISTENING_PORT = int(wsportplitted[1]) elif sys.argv[1].startswith('configfile'): wsportplitted = sys.argv[1].split('=') if len(wsportplitted) > 1: configfilepath = wsportplitted[1] fromconfigfilepathtoglobalserverparams(configfilepath) # open connection with redis server to query for keys and publish to channels redisconn = redis.lazyConnectionPool(REDIS_HOST, REDIS_PORT) # do the webservice server by listening to incoming messages on port WEBSERVICE_LISTENING_PORT ServerFactory = TwistedChatServerFactory factory = ServerFactory("ws://localhost:" + str(WEBSERVICE_LISTENING_PORT), debug=debug, debugCodePaths=debug) factory.protocol = TwistedChatServerProtocol factory.setProtocolOptions(allowHixie76=True) listenWS(factory) # connect to redis host for the publish/subscribe protocol TwistedRedisFactory = myRedisFactory() reactor.connectTCP(REDIS_HOST, REDIS_PORT, TwistedRedisFactory) # do the http server by listening to incoming messages on port 8080 #webdir = File(".") #web = Site(webdir)
import txredisapi as redis redis_store = redis.lazyConnectionPool(dbid=4, host='localhost', port=6379)
from twisted.internet import reactor, task, defer import struct import json from twisted.python import log import sys import time import txredisapi as redis log.startLogging(sys.stdout) REDIS_HOST = 'localhost' REDIS_PORT = 6379 REDIS_DB = 4 # REDIS_PASSWORD = '******' redis_store = redis.lazyConnectionPool(dbid=4, host='localhost', port=REDIS_PORT) # # @defer.inlineCallbacks def check_token(phone_number, token): token_in_redis = yield redis_store.hget('user:%s' % phone_number, 'token') if token != token_in_redis: defer.returnValue(False) else: defer.returnValue(True) class Chat(Protocol):
def render_GET(self, request): return "redis: %s\n" % repr(self.db) class XmlrpcHandler(BaseHandler, xmlrpc.XMLRPC): allowNone = True @defer.inlineCallbacks def xmlrpc_get(self, key): value = yield self.db.get(key) defer.returnValue(value) @defer.inlineCallbacks def xmlrpc_set(self, key, value): result = yield self.db.set(key, value) defer.returnValue(result) # redis connection _db = redis.lazyConnectionPool() # http resources root = Root() root.putChild("", IndexHandler(_db)) root.putChild("info", InfoHandler(_db)) root.putChild("xmlrpc", XmlrpcHandler(_db)) application = service.Application("webredis") srv = internet.TCPServer(8888, server.Site(root), interface="127.0.0.1") srv.setServiceParent(application)
def opened_spider(self, spider): self.rc = yield lazyConnectionPool(**self.config)