isefire

Rot13_App

Apr 9th, 2014
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.03 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. #  rot13.py
  5. #  
  6. #  Copyright 2014 [email protected]
  7. #  
  8. #  This program is free software; you can redistribute it and/or modify
  9. #  it under the terms of the GNU General Public License as published by
  10. #  the Free Software Foundation; either version 2 of the License, or
  11. #  (at your option) any later version.
  12. #  
  13. #  This program is distributed in the hope that it will be useful,
  14. #  but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. #  GNU General Public License for more details.
  17. #  
  18. #  You should have received a copy of the GNU General Public License
  19. #  along with this program; if not, write to the Free Software
  20. #  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  21. #  MA 02110-1301, USA.
  22. #  
  23. #  
  24. from escape_function import escape_html
  25. import webapp2
  26.  
  27. form = """
  28. <!DOCTYPE html>
  29.  
  30. <html>
  31.  <head>
  32.    <title>Unit 2 Rot 13</title>
  33.  </head>
  34.  
  35.  <body>
  36.    <h2>Enter some text to ROT13:</h2>
  37.    <form method="post">
  38.      <textarea name="text" style="height: 100px; width: 400px;">%(text)s</textarea>
  39.      <br>
  40.      <input type="submit">
  41.    </form>
  42.  </body>
  43.  
  44. </html>
  45. """
  46. class MainPage(webapp2.RequestHandler):
  47.     def write_form(self, text = ""):
  48.         self.response.out.write(form % {"text": escape_html(text)})
  49.        
  50.        
  51.     def get(self):
  52.         self.write_form()
  53.        
  54.     def post(self):
  55.         user_text = self.rot13a(self.request.get('text'))
  56.        
  57.         self.write_form(user_text)
  58.        
  59.     def rot13a(self, text):
  60.         abc = list("abcdefghijklmnopqrstuvwxyz")
  61.         ABC = list("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
  62.         lst = list(text)
  63.         newlst = []
  64.         for i in lst:
  65.             if i in abc:
  66.                 ind = abc.index(i)
  67.                 ind = (ind + 13)%26
  68.                 newlst.append(abc[ind])
  69.             elif i in ABC:
  70.                 ind = ABC.index(i)
  71.                 ind = (ind + 13)%26
  72.                 newlst.append(ABC[ind])
  73.             else:
  74.                 newlst.append(i)
  75.         txt = ""
  76.         text = txt.join(newlst)
  77.         return text
  78.        
  79.  
  80.  
  81. application = webapp2.WSGIApplication([('/', MainPage)],debug=True)
Advertisement
Add Comment
Please, Sign In to add comment