Python Bottle framework to implement the most basic get and post methods, pythonbottle
1. GET method:
#-*-Coding: UTF-8 -*-#! /Usr/bin/python # filename: GETPOST_test.py # codedtime: 2014-9-20 19: 07: 04 import bottledef check_login (username, password ): if username = '000000' and password = '000000': return True else: return False@bottle.route ('/login') def login (): if bottle. request. GET. get ('Do _ submit ',''). strip (): # click the logon button # method 1 (latin1 encoding) # username = bottle. request. GET. get ('username ',''). strip () # username # password = bottle. request. GET. get ('Password ',''). strip () # password # method 2 (get username \ password) (latin1 encoding) getValue = bottle. request. query_string ## username = bottle. request. query ['username'] # An utf8 string provisionally decoded as ISO-8859-1 by the server # password = bottle. request. query ['Password'] # Note: ISO-8859-1 (aka latin1 encoding) # Third method (get UTF-8 encoding) username = bottle. request. query. username # The same string correctly re-encoded as utf8 by bottle password = bottle. request. query. password # The same string correctly re-encoded as utf8 by bottle print ('getvalue = '+ getValue,' \ r \ nusername = '+ username, '\ r \ npassword =' + password) # test if check_login (username, password): return "<p> Your login information was correct. </p> "else: return" <p> Login failed. </p> "else: return ''' <form action ="/login "method =" get "> Username: <input name = "username" type = "text"/> Password: <input name = "password" type = "password"/> <input value = "Login" name = "do_submit" type = "submit"> </form> ''' bottle. run (host = 'localhost', port = 8083)
Here pay attention to the problem of Bottle encoding, only the third way will recode the character we enter if it is a UTF-8 to UTF-8, this method is especially important when your content contains Chinese or other non-English characters.
The running effect is as follows:
2. POST method:
#-*-Coding: UTF-8 -*-#! /Usr/bin/python # filename: GETPOST_test.py # codedtime: 2014-9-20 19: 07: 04 import bottledef check_login (username, password ): if username = '000000' and password = '000000': return True else: return False@bottle.route ('/login') def login (): return ''' <form action = "/login" method = "post"> Username: <input name = "username" type = "text"/> Password: <input name = "password" type = "password"/> <input value = "Login" type = "submit"> </form> ''' @ bottle. route ('/login', method = 'post') def do_login (): # method 1 # username = request. forms. get ('username') # password = request. forms. get ('Password') # method 2 postValue = bottle. request. POST. decode ('utf-8') username = bottle. request. POST. get ('username') password = bottle. request. POST. get ('Password') if check_login (username, password): return "<p> Your login information was correct. </p> "else: return" <p> Login failed. </p> "bottle. run (host = 'localhost', port = 8083)
We usually use the POST method instead of the GET method to log on to the website, submit articles, and comment, so encoding similar to the second method is very useful, the content we submitted in Form can be correctly processed. The first type may have the legend of Garbled text. Remember !!!