Skip to content

Instantly share code, notes, and snippets.

@jcarbaugh
Created April 4, 2009 04:13
Show Gist options
  • Save jcarbaugh/90108 to your computer and use it in GitHub Desktop.
Save jcarbaugh/90108 to your computer and use it in GitHub Desktop.
provides a HTTP and WSGI interface to memcached
#!/usr/bin/env python
import memcache
HOST = '127.0.0.1'
PORT = 11211
class QueueDoesNotExist(Exception):
pass
def application(environ, start_response):
path = environ['PATH_INFO']
method = environ['REQUEST_METHOD']
response_code = 200
content_type = 'text/plain'
try:
key = path.strip('/').replace('/','_')
if not key:
raise QueueDoesNotExist, "no queue specified"
c = memcache.Client(['%s:%d' % (HOST, PORT)])
if method in ('POST','PUT'):
post_data = environ['wsgi.input'].read()
if c.set(key, post_data):
response = ''
else:
response_code = 500
response = 'unable to write to queue: %s' % key
elif method in ('GET','HEAD'):
val = c.get(key)
if val:
response = val
else:
raise QueueDoesNotExist, key
else:
response_code = 405
response = "method not supported"
except QueueDoesNotExist, qdne:
response_code = 404
response = "queue does not exist: %s" % qdne.message
start_response(str(response_code), [('content-type', content_type)])
return (response,)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment