Skip to content

Instantly share code, notes, and snippets.

@deldotdr
Created January 25, 2010 23:15
Show Gist options
  • Save deldotdr/286376 to your computer and use it in GitHub Desktop.
Save deldotdr/286376 to your computer and use it in GitHub Desktop.
from twisted.web import resource
from twisted.web import rewrite
from twisted.web import server
from twisted.internet import reactor
class DataResource(resource.Resource):
def render(self, request):
"""request.args are where GET request args are stored.
(From docs) For example, for a URI with
'foo=bar&foo=baz&quux=spam'
for its query part, args will be
{'foo': ['bar', 'baz'], 'quux': ['spam']}
"""
print request.args
request.write(str(request.args))
return ""
def pathToArgs(request):
"""assume segments of path following "data" are values for the keys:
- sta
- orid
localhost:8008/data/109C/39495/
->
localhost:8008/data?sta=109C&orid=39495
"""
if request.postpath and request.postpath[0] == 'data':
sta = request.postpath[1]
orid = request.postpath[2]
request.args = {'sta':[sta], 'orid':[orid]}
request.postpath = ['data']
request.path = '/data'
root = resource.Resource()
root.putChild('data', DataResource())
rewrite_root = rewrite.RewriterResource(root, pathToArgs)
site = server.Site(rewrite_root)
reactor.listenTCP(8080, site)
reactor.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment