欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python的Bottle框架中實(shí)現(xiàn)最基本的get和post的方法的教程

 更新時(shí)間:2015年04月30日 17:27:16   作者:JohnnyHu90  
這篇文章主要介紹了Python的Bottle框架中實(shí)現(xiàn)最基本的get和post的方法的教程,Bottle框架在Python開發(fā)者中的人氣很高,需要的朋友可以參考下

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(): #點(diǎn)擊登錄按鈕
    # 第一種方式(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編碼的問題,只有第三種方式會(huì)將我們輸入的字符如果是UTF-8重新編碼為UTF-8,當(dāng)你的內(nèi)容里有中文或其他非英文字符時(shí),這種方式就顯的尤為重要。

運(yùn)行效果如下:

2015430172604482.png (699×104)

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)站、提交文章、評(píng)論等我們一般都會(huì)用POST方式而非GET方式,那么類似于第二種方式的編碼就很用用處,能夠正確的處理我們?cè)贔orm中提交的內(nèi)容。而第一種則可能會(huì)出現(xiàn)傳說中的亂碼問題,謹(jǐn)記!??!

相關(guān)文章

  • Python使用Matplotlib實(shí)現(xiàn)創(chuàng)建動(dòng)態(tài)圖形

    Python使用Matplotlib實(shí)現(xiàn)創(chuàng)建動(dòng)態(tài)圖形

    動(dòng)態(tài)圖形是使可視化更具吸引力和用戶吸引力的好方法,它幫助我們以有意義的方式展示數(shù)據(jù)可視化,本文將利用Matplotlib實(shí)現(xiàn)繪制一些常用動(dòng)態(tài)圖形,希望對(duì)大家有所幫助
    2024-02-02
  • python中如何使用正則表達(dá)式的集合字符示例

    python中如何使用正則表達(dá)式的集合字符示例

    我們都知道,正則表達(dá)式可以很方便地對(duì)字符串進(jìn)行匹配、查找、分割等操作,下面這篇文章主要給大家介紹了關(guān)于python中如何使用正則表達(dá)式的集合字符的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下。
    2017-10-10
  • 利用Python繪制虎年煙花秀

    利用Python繪制虎年煙花秀

    2022虎年新年即將來臨,小編為大家?guī)砹艘粋€(gè)利用Python編寫的虎年煙花特效,文中的示例代碼簡(jiǎn)潔易懂,感興趣的同學(xué)可以動(dòng)手試一試
    2022-01-01
  • 使用pyinstaller逆向.pyc文件

    使用pyinstaller逆向.pyc文件

    這篇文章主要介紹了使用pyinstaller逆向.pyc文件,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-12-12
  • python使用箱型圖剔除異常值的實(shí)現(xiàn)方法

    python使用箱型圖剔除異常值的實(shí)現(xiàn)方法

    python中的箱線圖可用于分析數(shù)據(jù)中的異常值,下面這篇文章主要給大家介紹了關(guān)于python使用箱型圖剔除異常值的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-05-05
  • python、Matlab求定積分的實(shí)現(xiàn)

    python、Matlab求定積分的實(shí)現(xiàn)

    今天小編就為大家分享一篇python、Matlab求定積分的實(shí)現(xiàn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2019-11-11
  • django框架模板中定義變量(set variable in django template)的方法分析

    django框架模板中定義變量(set variable in django template)的方法分析

    這篇文章主要介紹了django框架模板中定義變量(set variable in django template)的方法,結(jié)合實(shí)例形式分析了Django框架實(shí)現(xiàn)模板中定義變量與變量賦值相關(guān)操作技巧,需要的朋友可以參考下
    2019-06-06
  • 使用Python制作新型冠狀病毒實(shí)時(shí)疫情圖

    使用Python制作新型冠狀病毒實(shí)時(shí)疫情圖

    最近被新型冠狀病毒搞的人心惶惶,很多城市被病毒感染,今天小編給大家分享使用Python制作新型冠狀病毒實(shí)時(shí)疫情圖,感興趣的朋友跟隨小編一起看看吧
    2020-01-01
  • Python 私有化操作實(shí)例分析

    Python 私有化操作實(shí)例分析

    這篇文章主要介紹了Python 私有化操作,結(jié)合實(shí)例形式分析了Python私有屬性、私有方法相關(guān)使用技巧,需要的朋友可以參考下
    2019-11-11
  • python實(shí)現(xiàn)查詢IP地址所在地

    python實(shí)現(xiàn)查詢IP地址所在地

    本文給大家分享的是使用Python實(shí)現(xiàn)根據(jù)ip138的API查詢IP的地理位置的代碼,非常的實(shí)用,推薦給大家,有需要的小伙伴可以參考下。
    2015-03-03

最新評(píng)論