Python的Bottle框架中實現(xiàn)最基本的get和post的方法的教程
1、GET方式:
# -*- coding: utf-8 -*- #!/usr/bin/python # filename: GETPOST_test.py # codedtime: 2014-9-20 19:07:04 import bottle def check_login(username, password): if username == '123' and password == '234': return True else: return False @bottle.route('/login') def login(): if bottle.request.GET.get('do_submit','').strip(): #點擊登錄按鈕 # 第一種方式(latin1編碼) ## username = bottle.request.GET.get('username','').strip() # 用戶名 ## password = bottle.request.GET.get('password','').strip() # 密碼 #第二種方式(獲取username\password)(latin1編碼) 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'] # 注:ISO-8859-1(即aka latin1編碼) #第三種方式(獲取UTF-8編碼) 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)
這里注意說一下Bottle編碼的問題,只有第三種方式會將我們輸入的字符如果是UTF-8重新編碼為UTF-8,當你的內(nèi)容里有中文或其他非英文字符時,這種方式就顯的尤為重要。
運行效果如下:
2、POST方式:
# -*- coding: utf-8 -*- #!/usr/bin/python # filename: GETPOST_test.py # codedtime: 2014-9-20 19:07:04 import bottle def check_login(username, password): if username == '123' and password == '234': 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(): # 第一種方式 # username = request.forms.get('username') # password = request.forms.get('password') #第二種方式 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)
登錄網(wǎng)站、提交文章、評論等我們一般都會用POST方式而非GET方式,那么類似于第二種方式的編碼就很用用處,能夠正確的處理我們在Form中提交的內(nèi)容。而第一種則可能會出現(xiàn)傳說中的亂碼問題,謹記?。。?
- Python3.6通過自帶的urllib通過get或post方法請求url的實例
- python爬蟲中g(shù)et和post方法介紹以及cookie作用
- python利用urllib和urllib2訪問http的GET/POST詳解
- Python中用post、get方式提交數(shù)據(jù)的方法示例
- Python 使用requests模塊發(fā)送GET和POST請求的實現(xiàn)代碼
- python通過get,post方式發(fā)送http請求和接收http響應(yīng)的方法
- 正確理解Python中if __name__ == ''__main__''
- Python字典的核心底層原理講解
- Python對象與引用的介紹
- Python使用post及get方式提交數(shù)據(jù)的實例
相關(guān)文章
Python使用Matplotlib實現(xiàn)創(chuàng)建動態(tài)圖形
動態(tài)圖形是使可視化更具吸引力和用戶吸引力的好方法,它幫助我們以有意義的方式展示數(shù)據(jù)可視化,本文將利用Matplotlib實現(xiàn)繪制一些常用動態(tài)圖形,希望對大家有所幫助2024-02-02django框架模板中定義變量(set variable in django template)的方法分析
這篇文章主要介紹了django框架模板中定義變量(set variable in django template)的方法,結(jié)合實例形式分析了Django框架實現(xiàn)模板中定義變量與變量賦值相關(guān)操作技巧,需要的朋友可以參考下2019-06-06