Created
April 4, 2009 04:13
-
-
Save jcarbaugh/90108 to your computer and use it in GitHub Desktop.
provides a HTTP and WSGI interface to memcached
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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