def testPassCredentials(self, mocked_response):
        """
        If username/password passed to Client, auto-authenticate.
        """
        mocked_response.return_value = (0, 0, 0, 0, 0, 0, 0, 0, 0, [b'PLAIN'])
        client = bmemcached.Client('{}:11211'.format(
            os.environ['MEMCACHED_HOST']),
                                   username='******',
                                   password='******')
        server = list(client.servers)[0]

        # Force a connection.  Normally this is only done when we make a request to the
        # server.
        server._send_authentication()

        self.assertTrue(server.authenticated)
Пример #2
0
def get_memcache_client(herokuFlag=False):
    # Store IDs of comments that the bot has already replied to.
    # Read local cache by default

    MEMCACHEDCLOUD_SERVERS = '127.0.0.1:11211'
    MEMCACHEDCLOUD_USERNAME = '******'
    MEMCACHEDCLOUD_PASSWORD = '******'

    if heroku:
        MEMCACHEDCLOUD_SERVERS = os.environ.get('MEMCACHEDCLOUD_SERVERS')
        MEMCACHEDCLOUD_USERNAME = os.environ.get('MEMCACHEDCLOUD_USERNAME')
        MEMCACHEDCLOUD_PASSWORD = os.environ.get('MEMCACHEDCLOUD_PASSWORD')

    client = bmemcached.Client((MEMCACHEDCLOUD_SERVERS,), MEMCACHEDCLOUD_USERNAME,
                           MEMCACHEDCLOUD_PASSWORD)
    return client
Пример #3
0
def get_memcached_stats(conf):
    stats = {}
    # connect to memcached
    try:
        connection_string = "%s:%d" % (conf['host'], conf['port'])
        client = bmemcached.Client(connection_string, conf['instance'],
                                   EmptyString())
        log_verbose("Connection string=%s (instance=%s)" %
                    (connection_string, conf['instance']))
        for verb in ['', 'slabs', 'items']:
            s = client.stats(verb)
            assert (len(s) == 1)  # we have only one host
            stats[verb] = s[connection_string]
    except socket.error, e:
        collectd.error('memcached_stat plugin: Error connecting to %d - %r' %
                       (conf['port'], e))
Пример #4
0
def memcached_socket():
    """
    Start a standalone Memcached server.
    """
    try:
        host_socket_dir = os.path.realpath(tempfile.mkdtemp())
        host_socket_path = os.path.join(host_socket_dir, 'memcached.sock')
        os.chmod(host_socket_dir, 0777)

        env = os.environ
        env['PWD'] = HERE
        env['DOCKER_SOCKET_DIR'] = DOCKER_SOCKET_DIR
        env['DOCKER_SOCKET_PATH'] = DOCKER_SOCKET_PATH
        env['HOST_SOCKET_DIR'] = host_socket_dir

        docker_compose_file = os.path.join(HERE, 'compose',
                                           'docker-compose.yaml')
        subprocess.check_call(
            ["docker-compose", "-f", docker_compose_file, "down"])
        subprocess.check_call([
            "docker-compose", "-f", docker_compose_file, "up", "-d",
            "memcached_socket"
        ],
                              env=env)

        attempts = 0
        while True:
            if attempts > 10:
                raise Exception("Memcached boot timed out!")

            mc = bmemcached.Client(host_socket_path, USERNAME, PASSWORD)
            try:
                mc.set("foo", "bar")
            except MemcachedException:
                attempts += 1
                time.sleep(1)
            else:
                mc.delete("foo")
                mc.disconnect_all()
                break

        yield host_socket_dir, host_socket_path

        subprocess.check_call(
            ["docker-compose", "-f", docker_compose_file, "down"])
    finally:
        shutil.rmtree(host_socket_dir, ignore_errors=True)
Пример #5
0
    def testCasDelete(self):
        self.client = bmemcached.Client(self.server,
                                        'user',
                                        'password',
                                        socket_timeout=None)

        cas = 345
        self.assertTrue(self.client.cas('test_key_del', 'test1', cas))
        self.assertEqual(self.client.get('test_key_del'), 'test1')

        # If a different CAS value is supplied, the key is not deleted.
        self.assertFalse(self.client.delete('test_key_del', cas=cas + 1))
        self.assertEqual('test1', self.client.get('test_key_del'))

        # If the correct CAS value is supplied, the key is deleted.
        self.assertTrue(self.client.delete('test_key_del', cas=cas))
        self.assertEqual(None, self.client.get('test_key_del'))
Пример #6
0
    def __init__(self,
                 servers=None,
                 default_timeout=300,
                 key_prefix=None,
                 username=None,
                 password=None):
        BaseCache.__init__(self, default_timeout)

        if servers is None:
            servers = ['127.0.0.1:11211']

        import bmemcached
        self._client = bmemcached.Client(servers,
                                         username=username,
                                         password=password)

        self.key_prefix = key_prefix
Пример #7
0
    def _get_client(self):
        thread_id = threading.current_thread().ident
        if not hasattr(MemcachedCache, "_memcached_cache"):
            MemcachedCache._memcached_cache = {}

        if thread_id in MemcachedCache._memcached_cache:
            return MemcachedCache._memcached_cache[thread_id]

        servers = settings.RESTCLIENTS_MEMCACHED_SERVERS
        username = getattr(settings, "RESTCLIENTS_MEMCACHED_USER", None)
        password = getattr(settings, "RESTCLIENTS_MEMCACHED_PASS", None)

        client = bmemcached.Client(servers, username, password)

        MemcachedCache._memcached_cache[thread_id] = client

        return client
Пример #8
0
    def setUp(self):
        self._proxy_port = None

        # Start a helper to proxy requests to the actual memcache server.  This uses a
        # process instead of a thread, so we can simply kill the process between tests.
        self._start_proxy()
        self._stop_proxy()
        self._start_proxy()

        self.client = bmemcached.Client(self.server, 'user', 'password')

        # Disable retry delays, so we can disconnect and reconnect from the
        # server without needing to put delays in most of the tests.
        self.client.enable_retry_delay(False)

        # Clean up from any previous tests.
        self.client.delete('test_key')
        self.client.delete('test_key2')
Пример #9
0
 def __init__(self):
     try:
         import bmemcached
         #,,OCS_ACCESS_PASS
         self.client = bmemcached.Client((OCS_ACCESS_URL), OCS_ACCESS_ID,
                                         OCS_ACCESS_PASS)
         self.client.set('cache_conn', 'OK')
         if not self.client.get(
                 'cache_conn') or self.client.get('cache_conn') != 'OK':
             raise
         print 'aliyun ocs is connection...'
     except Exception:
         try:
             import memcache
             self.client = memcache.Client([MEMCACHE_HOST])
             print 'local memcache is connection...'
         except Exception:
             e = sys.exc_info()[0](traceback.format_exc())
Пример #10
0
    def check(self, instance):
        socket = instance.get('socket')
        server = instance.get('url')
        options = instance.get('options', {})
        username = instance.get('username')
        password = instance.get('password')

        if not server and not socket:
            raise Exception('Either "url" or "socket" must be configured')

        if socket:
            server = 'unix'
            port = socket
        else:
            port = int(instance.get('port', self.DEFAULT_PORT))
        custom_tags = instance.get('tags') or []

        mc = None  # client
        tags = ["url:{0}:{1}".format(server, port)] + custom_tags
        service_check_tags = ["host:%s" % server, "port:%s" % port] + custom_tags

        try:
            self.log.debug("Connecting to %s:%s tags:%s", server, port, tags)
            mc = bmemcached.Client(["{}:{}".format(server, port)], username, password)

            self._get_metrics(mc, tags, service_check_tags)
            if options:
                # setting specific handlers
                self.OPTIONAL_STATS["items"][2] = Memcache.get_items_stats
                self.OPTIONAL_STATS["slabs"][2] = Memcache.get_slabs_stats
                self._get_optional_metrics(mc, tags, options)
        except BadResponseError as e:
            self.service_check(
                self.SERVICE_CHECK, AgentCheck.CRITICAL,
                tags=service_check_tags,
                message="Unable to fetch stats from server")
            raise Exception(
                "Unable to retrieve stats from memcache instance: {}:{}."
                "Please check your configuration. ({})".format(server, port, e))

        if mc is not None:
            mc.disconnect_all()
            self.log.debug("Disconnected from memcached")
        del mc
Пример #11
0
def clear_database() -> None:
    # Hacky function only for use inside populate_db.  Designed to
    # allow running populate_db repeatedly in series to work without
    # flushing memcached or clearing the database manually.

    # With `zproject.test_settings`, we aren't using real memcached
    # and; we only need to flush memcached if we're populating a
    # database that would be used with it (i.e. zproject.dev_settings).
    if default_cache['BACKEND'] == 'django_bmemcached.memcached.BMemcached':
        bmemcached.Client(
            (default_cache['LOCATION'],), **default_cache['OPTIONS'],
        ).flush_all()

    model: Any = None  # Hack because mypy doesn't know these are model classes
    for model in [Message, Stream, UserProfile, Recipient,
                  Realm, Subscription, Huddle, UserMessage, Client,
                  DefaultStream]:
        model.objects.all().delete()
    Session.objects.all().delete()
Пример #12
0
    def testCas(self):
        self.client = bmemcached.Client(self.server,
                                        'user',
                                        'password',
                                        socket_timeout=None)

        cas = 345
        self.assertTrue(self.client.cas('test_key1', 'test1', cas))
        self.assertEqual(self.client.get('test_key1'), 'test1')

        # same cas
        self.assertTrue(self.client.cas('test_key1', 'test2', cas))
        self.assertEqual(self.client.get('test_key1'), 'test2')

        # new cas
        cas = 567
        self.assertFalse(self.client.cas('test_key1', 'test3', cas))
        # still equal to old value
        self.assertEqual(self.client.get('test_key1'), 'test2')
Пример #13
0
def main():
    """Run the bot."""
    run_on_start = RUN_ON_START

    memcache = bmemcached.Client(
        os.environ['MEMCACHEDCLOUD_SERVERS'].split(','),
        os.environ['MEMCACHEDCLOUD_USERNAME'],
        os.environ['MEMCACHEDCLOUD_PASSWORD'])
    logging.debug('Connected to memcache.')

    reddit = praw.Reddit(client_id=os.environ['REDDIT_CLIENT_ID'],
                         client_secret=os.environ['REDDIT_CLIENT_SECRET'],
                         password=os.environ['REDDIT_PASSWORD'],
                         user_agent=USER_AGENT,
                         username=os.environ['REDDIT_USERNAME'])
    reddit.validate_on_submit = True
    logging.info('Logged in.')

    subreddit = reddit.subreddit(SUBREDDIT)

    old_announcement_id = memcache.get('old_announcement_id')

    if RUN_FOREVER:
        while True:
            # Wait until it's time to run each day
            if run_on_start:
                logging.info('Forcing run due to RUN_ON_START.')
                run_on_start = False
            else:
                sleep_time = get_time_till_next_run()
                logging.info(
                    f'Sleeping for {int(sleep_time)} seconds ({round(sleep_time/(60**2), 2)} hours)'
                )
                time.sleep(sleep_time)

            run_once(reddit, subreddit, memcache, old_announcement_id)

            # Ensure no double dipping
            time.sleep(2)

    else:
        run_once(reddit, subreddit, memcache, old_announcement_id)
Пример #14
0
def load_properties():
    prop_file = "login_prop"
    if os.environ.get('MEMCACHEDCLOUD_SERVERS', None):
        import bmemcached
        mc = bmemcached.Client(
            os.environ.get('MEMCACHEDCLOUD_SERVERS').split(','),
            os.environ.get('MEMCACHEDCLOUD_USERNAME'),
            os.environ.get('MEMCACHEDCLOUD_PASSWORD'))
        obj = mc.get('props')
        if not obj:
            return {}
        else:
            return obj
    else:
        if os.path.isfile(prop_file):
            with open(prop_file, 'r+') as login_prop_file:
                data = pickle.load(login_prop_file)
                return data
        else:
            sys.exit("No prop file found")
Пример #15
0
    def __init__(
        self,
        host: str = "localhost",
        port: int = 11211,
        time: int = 432000,
        **kwargs: Dict,
    ):
        """
        Memcached-backed cache.

        Parameters
        ----------
        host: string, memcache host
        port: integer
        kwargs: passed directly to bmemcached.Client connection

        """
        # memcache
        self.memcache = bmemcached.Client((f"{host}:{port}", ), **kwargs)
        self.timeout = time
Пример #16
0
def test_end_to_end(memcached):
    config = {
        'MEMCACHED_URIS': [
            memcached,
        ]
    }

    container = ServiceContainer(ExampleService, config)
    container.start()

    # write through the service
    with entrypoint_hook(container, "write") as write:
        write("foobar")

    # verify changes written to memcached
    client = bmemcached.Client((memcached, ))
    assert client.get(TEST_KEY) == "foobar"

    # read through the service
    with entrypoint_hook(container, "read") as read:
        assert read() == "foobar"
Пример #17
0
    def login(self, ipaddress, port, user_passwd_pair_list):
        # 检查未授权访问功能
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        try:
            s.settimeout(self.timeout)
            s.connect((ipaddress, port))
            s.send('stats\r\n')
            tmp = s.recv(1024)
            if 'version' in tmp or b"version" in tmp:
                log_success("Memcached", ipaddress, port, None)
                return
        except Exception as e:
            pass
        finally:
            s.close()

        for user_passwd_pair in user_passwd_pair_list:
            try:
                client = bmemcached.Client(('{}:{}'.format(ipaddress, port), ),
                                           user_passwd_pair[0],
                                           user_passwd_pair[1],
                                           socket_timeout=self.timeout)
                status = client.stats()
                data = json.dumps(status.get("{}:{}".format(ipaddress, port)))
                if 'Auth failure' in data:
                    continue
                elif "version" in data:
                    log_success("Memcached", ipaddress, port, user_passwd_pair)
                else:
                    return

            except Exception as E:
                logger.debug('AuthenticationException: %s' % E)
                continue
            finally:
                pass
Пример #18
0
 def testAcceptStringServer(self):
     client = bmemcached.Client('{}:11211'.format(
         os.environ['MEMCACHED_HOST']))
     self.assertEqual(len(list(client.servers)), 1)
Пример #19
0
 def connect(self):
     return bmemcached.Client(self.servers,
                              username=self.username,
                              password=self.password)
Пример #20
0
def client():
    return bmemcached.Client(["{}:{}".format(HOST, PORT)], USERNAME, PASSWORD)
Пример #21
0
def client_socket(memcached_socket):
    return bmemcached.Client(memcached_socket[1], USERNAME, PASSWORD)
Пример #22
0
from models import User, Match, db, CachedYelpPlace, Wager
from peewee import IntegrityError, DoesNotExist
from ranking import calculate_ranks
from profile_matches import get_my_matches
from yelp import YelpFusionHandler

app = Flask(__name__)
BCRYPT = Bcrypt(app)

# Memcache Setup
if "HEROKU" not in os.environ:
    # Get from credentials file
    MEMCACHE_CREDENTIALS = json.loads(
        file("credentials.json").read())["memcache"]
    MC = bmemcached.Client(MEMCACHE_CREDENTIALS["host"],
                           MEMCACHE_CREDENTIALS["username"],
                           MEMCACHE_CREDENTIALS["password"])
else:
    # Get data from Heroku
    MC = bmemcached.Client(os.environ["MEMCACHE_HOST"],
                           os.environ["MEMCACHE_USERNAME"],
                           os.environ["MEMCACHE_PASSWORD"])

# Cookie expiry
SIXTY_DAYS = datetime.now() + timedelta(days=60)

# Memcache key expiry
TWENTY_MIN = 600


@app.before_request
Пример #23
0
import bmemcached


print("\nconnecting to redis labs memcached cloud...")	

# create a new client
#client = bmemcached.Client(('pub-memcache-17991.us-east-1-1.2.ec2.garantiadata.com:17991', ), 'eran','rs87362c')
client = bmemcached.Client(('pub-memcache-11977.us-east-1-1.2.ec2.garantiadata.com:11977', ), 'eran','rs87362c')

print("\nset test key...")
print("\n>r.set(`foo`, `bar`)")
client.set('foo', 'bar')

print("\nget test key...")
print("\n>r.get(`foo`)")
print client.get('foo')
Пример #24
0
# Main method
if __name__ == "__main__":
    # If the bot is already running
    if os.path.isfile('BotRunning'):
        log("The bot is already running, shutting down", Color.RED)
        exit_bot()

    if os.environ.get('MEMCACHEDCLOUD_SERVERS', None):
        import bmemcached

        log('Running on heroku, using memcached', Color.BOLD)

        running_on_heroku = True
        mc = bmemcached.Client(
            os.environ.get('MEMCACHEDCLOUD_SERVERS').split(','),
            os.environ.get('MEMCACHEDCLOUD_USERNAME'),
            os.environ.get('MEMCACHEDCLOUD_PASSWORD'))

    # Register the function that get called on exit
    atexit.register(exit_handler)

    # Register function to call on SIGINT
    signal.signal(signal.SIGINT, signal_handler)

    # The bot was not running
    # create the file that tell the bot is running
    open('BotRunning', 'w').close()

    log("Starting Bot", Color.BOLD)

    log("OS is " + sys.platform, Color.BOLD)
Пример #25
0
import bmemcached
import sys

host = "localhost"
host = sys.argv[1] if len(sys.argv) > 1 else host
result = 'success'
#port = 11211
client = bmemcached.Client('%s:11211' % host)
if not client.set('key1', 'value1'):
    result = 'failed'
if client.get('key1') != 'value1':
    result = 'failed'
print "result:" + str(result)
print "key1:" + client.get('key1')
Пример #26
0
for i in range(testcount):
    key = random_generator(size=6)
    value = random_generator()
    my_dict[key] = value

print "Dict Ready Start testing"

rand_dict = RandomDict(my_dict)
if len(sys.argv) > 2:
    port = int(sys.argv[2])
else:
    port = 5000
server = "127.0.0.1:" + str(port)
print server
client = bmemcached.Client(server,
                           username=None,
                           password=None,
                           socket_timeout=10000)
#client = bmemcached.Client('127.0.0.1:2041',username=None, password=None, socket_timeout=10000)

setpass = 0
setfailed = 0
n = 0
for k in my_dict.keys():
    n += 1
    result = client.set(k, my_dict[k])
    if result is not True:
        setfailed += 1
        #print "pass : "******" failed for ", k
    else:
        setpass += 1
    if n % 1000 == 0:
Пример #27
0
 def testAcceptUnixSocket(self):
     client = bmemcached.Client('/tmp/memcached.sock')
     self.assertEqual(len(list(client.servers)), 1)
Пример #28
0
 def testAcceptIterableServer(self):
     client = bmemcached.Client([
         '{}:11211'.format(os.environ['MEMCACHED_HOST']),
         '{}:11211'.format(os.environ['MEMCACHED_HOST'])
     ])
     self.assertEqual(len(list(client.servers)), 2)
Пример #29
0
# pip install python-binary-memcached
import logging
import bmemcached

#logging.basicConfig()
#logging.getLogger('bmemcached').setLevel(logging.DEBUG)

client = bmemcached.Client('127.0.0.1:3000')

client.set('hello', 'world')
print client.get('hello')

client.set('hello', 1)
print client.get('hello')

client.set('hello', ['world1', 'world2'])
a = client.get('hello')
print a
print len(a)
print isinstance(a, list)
print isinstance(a, basestring)

Пример #30
0
# -*- coding: utf-8 -*-
import bmemcached
import time
import random

# Tested at python27

client = bmemcached.Client(('10.235.65.18:11211'), 'bmemcached', 'bmemcached')

i = 0
while (i < 10000):
    print(i)

    t = time.time()
    #print(type(t), t)
    s = lambda: int(round(t * 1000000))
    s = s()
    print(type(s), s)

    k = 'ck_t_mc_' + str(s)
    print(type(k), k)

    res_set = client.set(k, s, 86400)
    print(res_set)

    res_get = client.get(k)
    print(res_get)

    i += 1