1
import webapp2
import cgi

def escape_html(s):
    return cgi.escape(s, quote = True)

def rot13(s):
    rot13Text = []
    for i in s:
        if ord(i) <= ord('M') or ord(i) <= ord('m'):
            rot13Text.append(chr(ord(i)+13))
        else:
            rot13Text.append(chr(ord(i)-12))
    return rot13Text

form = """
<form method="post">
    What is your birthday?
    <br>
    <label>
        Month
        <input type="text" name="month" value="%(month)s">
    </label>
    <label>
        Day
        <input type="text" name="day" value="%(day)s">
    </label>
    <label>
        Year
        <input type="text" name="year" value="%(year)s">
    </label>
    <div style="color: red">%(error)s</div>
    <br><br>
    <input type="submit">
</form>
"""

rot13Form = """
    <textarea method="post" name = "text" value="%(text)s"></textarea>
    <input type="submit">
"""

months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August',
                        'September', 'October', 'November', 'December']

def valid_day(day):
        if(day and day.isdigit()):
                day = int(day)
        if(day < 32 and day > 0):
                return day

def valid_month(month):
        if(month):
                month = month.capitalize()
        if(month in months):
                return month

def valid_year(year):
        if(year and year.isdigit()):
                year = int(year)
        if(year < 2020 and year > 1980):
                return year

class MainPage(webapp2.RequestHandler):
    def write_form(self, error="", month="", day="", year=""):
        self.response.out.write(form %{"error": error,
                                       "month": escape_html(month),
                                       "day": escape_html(day),
                                       "year": escape_html(year)})

    def get(self):
        self.write_form()

    def post(self):
        user_month = self.request.get('month')
        user_day = self.request.get('day')
        user_year = self.request.get('year')

        month = valid_month(user_month)
        day = valid_day(user_day)
        year = valid_year(user_year)

        if not(month and day and year):
            self.write_form("That doesn't look valid to me, friend.", user_month, user_day, user_year)
        else:
            self.redirect("/thanks")

class ThanksHandler(webapp2.RequestHandler):
    def get(self):
        self.response.out.write("Thanks! That's a totally valid day!")


class Rot13Handler(webapp2.RequestHandler):
    def writeForm(self, text = ""):
        self.response.out.write(rot13Form % {"text" : escape_html(text)}) 
    def get(self):
        self.writeForm()

    def post(self):
        userText = self.request.get('text')
        self.writeForm(rot13(userText))

app = webapp2.WSGIApplication([('/', MainPage),
                              ('/thanks', ThanksHandler),
                              ('/rot13', Rot13Handler)],
                             debug=True)

我正在尝试 rot13 文本区域中的一些文本。我不知道为什么当我点击提交时文本没有改变

4

0 回答 0