Created
November 14, 2016 14:10
-
-
Save shisaq/5fc4e7ed11f4f0d4d69e975c06706270 to your computer and use it in GitHub Desktop.
a simple example to realize "rot13" encode
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 "&" -- "&" | |
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