Skip to content

Instantly share code, notes, and snippets.

@davidkrisch
Last active September 20, 2017 20:38
Show Gist options
  • Save davidkrisch/abb14bbf6e2f5a64d2bd061c382b0544 to your computer and use it in GitHub Desktop.
Save davidkrisch/abb14bbf6e2f5a64d2bd061c382b0544 to your computer and use it in GitHub Desktop.
A web server that prints POST requests to stdout
#!/usr/bin/env python
# Print POST request body to console
#
# To run it..
# virtualenv env
# source ./env/bin/activate
# pip install cherrypy
# python main.py
#
# To test it...
# curl -X POST -d '["aaaa", ["spam", ["eggs", ["lumberjack", ["knights", ["ni", ["dead", ["parrot", ["fresh fruit"]]]]]]]], ["aaa", "bbb"], ["cccc", "dddd"]]' -H "Content-Type: application/json" localhost:8081
import pprint
import cherrypy
@cherrypy.expose
class ToConsole(object):
@cherrypy.tools.accept(media='text/plain')
def GET(self):
return 'Hello'
@cherrypy.tools.json_in()
def POST(self):
pprint.pprint(cherrypy.request.json)
return 'OK'
if __name__ == '__main__':
conf = {
'/': {
'request.dispatch': cherrypy.dispatch.MethodDispatcher(),
'tools.response_headers.on': True,
'tools.response_headers.headers': [('Content-Type', 'text/plain')],
}
}
cherrypy.config.update({'server.socket_host': '0.0.0.0', 'server.socket_port': 8081})
cherrypy.quickstart(ToConsole(), '/', conf)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment