最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

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

相關(guān)文章

  • Pandas Describe函數(shù)的具體使用

    Pandas Describe函數(shù)的具體使用

    在Pandas中,describe()能夠?yàn)閿?shù)據(jù)框中的數(shù)值列提供統(tǒng)計摘要信息,本文主要介紹了Pandas Describe函數(shù)的具體使用,具有一定的參考價值,感興趣的可以了解一下
    2024-02-02
  • Python?list列表查找元素詳情

    Python?list列表查找元素詳情

    這篇文章主要介紹了Python?list列表查找元素詳情,Python?列表(list)提供了?index和count方法,它們都可以用來查找元素,文章圍繞主題的相關(guān)資料展開詳細(xì)的內(nèi)容介紹,具有一定的參考價價值,需要的朋友可以參考一下
    2022-06-06
  • python中的字符串占位符的"{0:2}"

    python中的字符串占位符的"{0:2}"

    這篇文章主要介紹了python中的字符串占位符的"{0:2}",具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-05-05
  • python實(shí)現(xiàn)程序重啟和系統(tǒng)重啟方式

    python實(shí)現(xiàn)程序重啟和系統(tǒng)重啟方式

    這篇文章主要介紹了python實(shí)現(xiàn)程序重啟和系統(tǒng)重啟方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-04-04
  • python刪除目錄的三種方法

    python刪除目錄的三種方法

    本文主要介紹了python刪除目錄的三種方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-12-12
  • python?pandas?query的使用方法

    python?pandas?query的使用方法

    這篇文章主要介紹了python?pandas?query的使用方法,文章通過圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,感興趣的小伙伴可以參考一下
    2022-09-09
  • Python pip替換為阿里源的方法步驟

    Python pip替換為阿里源的方法步驟

    這篇文章主要介紹了Python pip替換為阿里源的方法步驟,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07
  • Python 逐行分割大txt文件的方法

    Python 逐行分割大txt文件的方法

    本文通過代碼給大家介紹了Python 逐行分割大txt文件的方法,在文中給大家提到了Python從txt文件中逐行讀取數(shù)據(jù)的方法,需要的朋友參考下吧
    2017-10-10
  • PyTorch實(shí)現(xiàn)卷積神經(jīng)網(wǎng)絡(luò)的搭建詳解

    PyTorch實(shí)現(xiàn)卷積神經(jīng)網(wǎng)絡(luò)的搭建詳解

    這篇文章主要為大家介紹了PyTorch實(shí)現(xiàn)卷積神經(jīng)網(wǎng)絡(luò)的搭建詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-05-05
  • 使用python分析統(tǒng)計自己微信朋友的信息

    使用python分析統(tǒng)計自己微信朋友的信息

    這篇文章主要介紹了python分析統(tǒng)計自己微信朋友的信息,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-07-07

最新評論

临武县| 江都市| 宝鸡市| 泰安市| 上林县| 崇明县| 宜丰县| 北流市| 鹤峰县| 宝丰县| 乌海市| 陆丰市| 永嘉县| 留坝县| 沙河市| 大新县| 宽城| 永靖县| 林周县| 定结县| 定结县| 丽水市| 太康县| 东辽县| 精河县| 翁源县| 牙克石市| 新兴县| 娄底市| 墨江| 开封县| 普洱| 浮梁县| 襄樊市| 新宾| 灵台县| 绿春县| 江安县| 柳州市| 江北区| 永善县|