Skip to content

Instantly share code, notes, and snippets.

@shisaq
Created November 14, 2016 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shisaq/5fc4e7ed11f4f0d4d69e975c06706270 to your computer and use it in GitHub Desktop.
Save shisaq/5fc4e7ed11f4f0d4d69e975c06706270 to your computer and use it in GitHub Desktop.
a simple example to realize "rot13" encode
import webapp2
import cgi
import codecs
# build a simple html form
form = """
<form method="post">
<h1 style="color: green">Let's use rot13 cipher!</h1>
<textarea name="text" rows="8" cols="40">%(text)s</textarea>
<input type="submit">
</form>
"""
# build a function that make the special characters into character entities, such like "&" -- "&amp;"
def escape_html(s):
return cgi.escape(s, quote = True)
class MainPage(webapp2.RequestHandler):
def write_form(self, text=""):
self.response.out.write(form % {'text': escape_html(text)})
def get(self):
self.write_form()
def post(self):
user_text = self.request.get('text')
# 'ignore' is to ignore Chinese characters
translated_text = codecs.encode(user_text, 'rot_13', 'ignore')
self.write_form(translated_text)
app = webapp2.WSGIApplication([
('/', MainPage),
], debug=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment