Skip to content

Instantly share code, notes, and snippets.

@jirihnidek
Last active August 29, 2015 14:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jirihnidek/850a8d77c47e14e6708e to your computer and use it in GitHub Desktop.
Save jirihnidek/850a8d77c47e14e6708e to your computer and use it in GitHub Desktop.
WebPy JSON Example
"""
This module tries to print JSON from HTTP GET/POST method.
"""
#
# To test the application run application with:
#
# python ./webpy_json.py
#
# and type following URI in browser addres bar:
#
# http://localhost:8080/json/{"name": "Joe"}
#
import web
import json
URLS = (
'/', 'HandleIndex',
'/json/(.*)', 'HandleJSON'
)
# Only for testing purpose. Do not use in production!
LAST_JSON_DOC = 'No JSON document.'
class HandleIndex(object):
"""
Application handling index
"""
def __init__(self):
"""
Constructor of HandleIndex
"""
print "Starting index app ..."
def __del__(self):
"""
Destructor of HandleIndex
"""
print "Ending index app ..."
def GET(self):
"""
Callback method handling HTTP GET request
"""
return LAST_JSON_DOC
class HandleJSON(object):
"""
Aplication handling JSON
"""
def __init__(self):
"""
Constructor of HandleJSON
"""
print "Starting JSON app ..."
def __del__(self):
"""
Destructor of HandleIndex
"""
print "Ending JSON app ..."
def GET(self, res):
"""
Callback method handling HTTP GET request
"""
global LAST_JSON_DOC
LAST_JSON_DOC = json.loads(res)
return LAST_JSON_DOC
def POST(self, res):
"""
Callback method handling HTTP POST request
"""
global LAST_JSON_DOC
json_data = web.data()
LAST_JSON_DOC = json.loads(json_data)
return LAST_JSON_DOC
def main():
"""
Main function starting app
"""
http_app = web.application(URLS, globals())
http_app.run()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment