RFC3261 python原始碼分析 2

2021-06-21 07:38:18 字數 2248 閱讀 7186

#----------------------- header and message -------------------------------

_quote = lambda s: '"' + s + '"' if s[0] != '"' != s[-1] else s

_unquote = lambda s: s[1:-1] if s[0] == '"' == s[-1] else s

這兩個lambda函式用來加上和去除引號。 python支援用lambda定義單行函式,該函式物件又可以賦值給變數。

# various header types: standard (default), address, comma and unstructured

_address = ['contact', 'from', 'record-route', 'refer-to', 'referred-by', 'route', 'to']

_comma = ['authorization', 'proxy-authenticate', 'proxy-authorization', 'www-authenticate']

_unstructured = ['call-id', 'cseq', 'date', 'expires', 'max-forwards', 'organization', 'server', 'subject', 'timestamp', 'user-agent']

# short form of header names

_short = ['allow-events', 'u', 'call-id', 'i', 'contact', 'm', 'content-encoding', 'e', 'content-length', 'l', 'content-type', 'c', 'event', 'o', 'from', 'f', 'subject', 's', 'supported', 'k', 'to', 't', 'via', 'v']

# exception for canonicalization of header names

_exception =

#_canon = lambda s: '-'.join([x.capitalize() for x in s.split('-')]) if s.lower() not in ['cseq','call-id','www-authenticate'] else [s.lower()]

上面分別用列表和元組儲存一些特殊的header

def _canon(s):

'''return the canonical form of the header.

>>> print _canon('call-id'), _canon('from'), _canon('refer-to')

call-id from refer-to

'''s = s.lower()

return ((len(s)==1) and s in _short and _canon(_short[_short.index(s)-1])) \

or (s in _exception and _exception[s]) or '-'.join([x.capitalize() for x in s.split('-')])

上面**定義了乙個函式。作用是對字串變化大小寫,輸出駱駝體格式的字串。

比如"abc"會輸出"abc"。這裡最後的return語句比較複雜。通過幾個用例可以理解這段**:

>>> _canon('u')

'allow-events'

((len(s)==1) and s in _short and _canon(_short[_short.index(s)-1]))

>>> _canon('call-id')

'call-id'

s in _exception and _exception[s]

>>> _canon('hello-world')

'hello-world'

>>> _canon('abc')

'abc'

>>> _canon("a-b-c")

'a-b-c'

'-'.join([x.capitalize() for x in s.split('-')])

python 執行緒池 python執行緒池原始碼解析

本篇主要講下threadpoolexecutor的實現。由於業務量不大,且一直使用框架進行程式設計,對執行緒的理解一直很模糊,基本處於不想阻塞程式執行,起乙個執行緒啟動任務的階段。總感覺自己好像會執行緒一樣,實則一直處於一種懵懂狀態,通過一段時間檢視一些別人寫的原始碼,終於有所悟,也記錄下自己的學習...

親測Python編寫tb搶購指令碼,文末原始碼

測試過程 github位址 原始碼在文章末尾 原始碼用於技術交流,切勿傳播,責任自負。原始碼用於技術交流,切勿傳播,責任自負。原始碼用於技術交流,切勿傳播,責任自負。windows 系統 python 環境 chrome 瀏覽器 程式設計基礎,要看懂原始碼 import time import da...

用Python畫一顆心 小人發射愛心(附原始碼)

本文出自 python為什麼 系列,歸檔在 github 上 毫無疑問,python 是一門強型別語言。強型別語言。強型別語言!關於強弱型別話題,推薦閱讀這篇 技術科普文 這就意味著,不同型別的物件通常需要先做顯式地型別轉化,然後才能進行某些操作。下面以字串和數字為例,看看強行操作會產生什麼結果 p...