乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      用 Python 描述 Cookie 和 Session

       鷹兔牛熊眼 2019-02-26

      Illustrations by Artur Sadlos

      ?

      作者:愛(ài)看七龍珠

      博客:zhihu.com/c_1046065830684614656

      這篇文章我們來(lái)聊聊Cookie和Session,網(wǎng)上有很多關(guān)于這兩個(gè)知識(shí)點(diǎn)的描述,可惜的是大部分都沒(méi)有示例代碼,因此本文的重點(diǎn)在于示例代碼。

      環(huán)境

      Python3.6.0
      Bottle0.12.15

      安裝bottle

      pip install bottle

      Cookie

      HTTP是一種無(wú)狀態(tài)協(xié)議,簡(jiǎn)單來(lái)說(shuō)就是如果A第一次訪問(wèn)了B服務(wù)器,那么A第二次訪問(wèn)B服務(wù)器時(shí),B服務(wù)器并不知道這次訪問(wèn)是否還是來(lái)自A。B服務(wù)器只負(fù)責(zé)接收網(wǎng)絡(luò)信息包,傳遞網(wǎng)絡(luò)信息包。這樣速度很快,但是很不方便,B服務(wù)器不會(huì)記錄A的數(shù)據(jù)。

      為此,人們發(fā)明了Cookie,Cookie利用了HTTP中的Headers字段

      HTTP/1.1 200 OK
      Date: Mon, 23 May 2005 22:38:34 GMT
      Content-Type: text/html; charset=UTF-8
      Content-Length: 138
      Last-Modified: Wed, 08 Jan 2003 23:11:55 GMT
      Server: Apache/1.3.3.7 (Unix) (Red-Hat/Linux)
      ETag: '3f80f-1b6-3e1cb03b'
      Accept-Ranges: bytes
      Connection: close
      cookie: _zap=8aa393b0-cc62-4c6a

      現(xiàn)在A每次訪問(wèn)時(shí)都帶上cookie,B服務(wù)器就可以獲得A的cookie,這樣B服務(wù)器就能記住一些東西

      假如說(shuō)我們現(xiàn)在要統(tǒng)計(jì)A一共訪問(wèn)了幾次B服務(wù)器,我們可以編寫以下代碼

      cookie.py

      from bottle import route, run, response, request

      @route('/hello')
      def hello():
          count = request.get_cookie('visited')
          if count:
              increment = int(count) + 1
              response.set_cookie('visited', str(increment))
              return str(increment)
          else:
              response.set_cookie('visited', '0')
              return 'Hello, World'

      run(host='localhost', port=8080, debug=True)

      需要說(shuō)明一點(diǎn):在整個(gè)傳輸過(guò)程中傳輸?shù)氖亲址绻麄鬟M(jìn)去一個(gè)整數(shù),那么會(huì)報(bào)錯(cuò),因此數(shù)字必須轉(zhuǎn)換為字符

      執(zhí)行 python cookie.py

      Session

      Session一般用于登錄功能,我們利用Cookie實(shí)現(xiàn)了有狀態(tài)的傳輸,那么完全可以設(shè)置一個(gè)id,每次訪問(wèn)的時(shí)候都會(huì)帶上這個(gè)id,這樣B服務(wù)器就能夠識(shí)別是誰(shuí)訪問(wèn)了

      一般來(lái)說(shuō),拿到id之后會(huì)存儲(chǔ)在數(shù)據(jù)庫(kù)里面,為了方便起見(jiàn),在這里只存儲(chǔ)到字典里面

      session.py

      from bottle import route, run, response, request, redirect

      login_user = {} #用來(lái)存儲(chǔ)登錄狀態(tài)的字典

      @route('/login')
      def login():
          key = hash('test password')
          login_user[key] = 'test password'
          response.set_cookie('login', str(key))
          return 'login successfuly!'

      @route('/logout')
      def logout():
          key = request.get_cookie('login')
          login_user.pop(int(key), None)
          return 'logout successfuly!'

      @route('/logintest')
      def logintest():
          key = request.get_cookie('login')
          if key is not None and int(key) in login_user:
              return 'login test successfuly!'
          else :
              return redirect('/beforelogin')

      @route('/beforelogin')
      def beforelogin():
          return 'please login!'

      run(host='localhost', port=8080, debug=True)

      執(zhí)行

      python session.py


        本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評(píng)論

        發(fā)表

        請(qǐng)遵守用戶 評(píng)論公約

        類似文章 更多