Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
52 lines (43 sloc) 1.54 KB
#!/usr/bin/env python
import os
import bmemcached
def connect():
# read memcached connection parameters from environment
memcached_endpoint = '%s:%s' % (os.environ['OPENSHIFT_MEMCACHED_HOST'], os.environ['OPENSHIFT_MEMCACHED_PORT'])
memcached_username = os.environ['OPENSHIFT_MEMCACHED_USERNAME']
memcached_password = os.environ['OPENSHIFT_MEMCACHED_PASSWORD']
# connect to memcached
return bmemcached.Client((memcached_endpoint,), memcached_username, memcached_password)
def application(environ, start_response):
status = '200 OK'
components = environ['PATH_INFO'].strip('/').split('/')
gear_id = os.environ['OPENSHIFT_GEAR_UUID']
if len(components) == 2:
action, name = components
assert action == 'get'
# read key value
client = connect()
value = client.get(name)
response_body = '%s (gear-id=%s)\n' % (value, gear_id)
client.disconnect_all()
elif len(components) == 3:
action, name, value = components
assert action == 'set'
# save key value
client = connect()
client.set(name, value)
client.disconnect_all()
response_body = 'OK (gear-id=%s)\n' % gear_id
else:
response_body = 'Valid URLs:\n /set/{name}/{value}\n /get/{name}\n'
response_headers = [('Content-Type', 'text/plain'), ('Content-Length', str(len(response_body)))]
start_response(status, response_headers)
return [response_body]
#
# Below for testing only
#
if __name__ == '__main__':
from wsgiref.simple_server import make_server
httpd = make_server('localhost', 8051, application)
# Wait for a single request, serve it and quit.
httpd.handle_request()