Skip to content

Instantly share code, notes, and snippets.

@Sonophoto
Created December 12, 2017 22:57
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Sonophoto/39818f1cee1ab4b6fedf010ecc7b55ae to your computer and use it in GitHub Desktop.
Save Sonophoto/39818f1cee1ab4b6fedf010ecc7b55ae to your computer and use it in GitHub Desktop.
Simple example of GET and POST with Cherrypy
import cherrypy
html_header = \
"""
<html>
<head>
<title>Example of GET and POST</title>
</head>
<body>
"""
html_footer = \
"""
<p><strong>Powered by:<a href="http://cherrypy.org/"><em>Cherrypy</em> A Minimalist Python Web Framework</a></p>
</body>
</html>
"""
class GetPostMethods(object):
@cherrypy.expose
def index(self):
html_body = \
"""
<p>
<form method="get" action="get_hello">
<input type="text" value="" name="get_name" />
<button type="submit">GET Hello!</button>
</form>
</p><p>
<form method="post" action="post_hello">
<input type="text" value="" name="post_name"/>
<button type="submit">POST Hello!</button>
</form>
"""
return html_header + html_body + html_footer
@cherrypy.expose
def get_hello(self, get_name):
# cherrypy.request.method == 'GET'
html_body = "<p>Hello %s!</p>" % get_name #NOTE This input IS NOT SANITIZED!!!
return html_header + html_body + html_footer
@cherrypy.expose
def post_hello(self, post_name):
# if cherrypy.request.method == 'POST':
html_body = "<p>Hello %s!</p>" % post_name #NOTE This input IS NOT SANITIZED!!!
return html_header + html_body + html_footer
if __name__ == '__main__':
conf = { '/': {}
}
cherrypy.quickstart(GetPostMethods(), '/', conf)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment