BaseHTTPRequestHandler handle() handle_one_request() method()

來源:互聯網
上載者:User
Python標準庫源碼分析:BaseHTTPServer.py

從SocketServer.py分析中可以知道其設計思想是將socket編程的監聽迴圈和用戶端處理劃分成Server類和RequestHandler類,而BaseHTTPServer是基於SocketServer基礎之上的,因此可以知道BaseHTTPServer就是分別擴充Server類和RequestHandler類。BaseHTTPServer實現了一個簡單的HTTP Server,可以知道主要工作應該是擴充RequestHandler的功能,處理用戶端的HTTP請求,具體包括HTTP協議解析,給用戶端返回HTTP響應,日誌記錄等功能。 1.HTTPServer

該類只是簡單封裝了SocketServer中的TCPServer[1]類 2.BaseHTTPRequestHandler

File "/usr/local/lib/python2.6/threading.py", line 504, in __bootstrap
    self.__bootstrap_inner()
  File "/usr/local/lib/python2.6/threading.py", line 532, in __bootstrap_inner
    self.run()
  File "/usr/local/lib/python2.6/threading.py", line 484, in run
    self.__target(*self.__args, **self.__kwargs)
  File "/usr/local/lib/python2.6/SocketServer.py", line 560, in process_request_thread
    self.finish_request(request, client_address)
  File "/usr/local/lib/python2.6/SocketServer.py", line 322, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "/usr/local/lib/python2.6/SocketServer.py", line 617, in __init__
    self.handle()
  File "/usr/local/lib/python2.6/BaseHTTPServer.py", line 329, in handle
    self.handle_one_request()
  File "/usr/local/lib/python2.6/BaseHTTPServer.py", line 323, in handle_one_request
    method()

類定義

class BaseHTTPRequestHandler(SocketServer.StreamRequestHandler):

擴充RequestHandler需要覆蓋一個介面handler(),定義如下:

 def handle(self): """Handle multiple requests if necessary.""" self.close_connection = 1 
self
.handle_one_request() while not self.close_connection: self.handle_one_request() 

主要處理的是HTTP是否保持串連的問題,如果保持串連就持續處理客戶請求,否則就結束了。
handle_one_request()比較重要語句:

 self.raw_requestline = self.rfile.readline(65537) 
self.parse_request() mname = 'do_' + self.command if not hasattr(self, mname) self.send_error(501, "Unsupported method (%r)" % self.command) return 
method = getattr(self, mname) 
method() self.wfile.flush() #actually send the response if not already done. 

讀取第一行raw_requestline,一般格式應該是:COMMAND PATH VERSION\r\n;解析請求,下面具體分析;後面幾行代碼意思是根據HTTP方法擷取對應處理函數,其實就是根據GET或POST請求調用對應的do_GET()或do_POST()方法,然後重新整理輸出。
parse_request()主要的代碼都在處理raw_requestline,最終得到self.command, self.path, self.request_version幾個變數,然後就是利用mimetools.Message解析頭部。
至此主體流程就結束了。
下面還有幾個的函數,功能都比較簡單:

 def send_error(self, code, message=None): def send_response(self, code, message=None): def send_header(self, keyword, value): def end_headers(self): def log_request(self, code='-', size='-'): def log_error(self, format, *args): def log_message(self, format, *args): 

總結:可以看到,BaseHTTPRequestHandler主要實現了用戶端HTTP請求解析,以及一些協助工具功能,如日誌記錄、錯誤處理、發送響應代碼等。但是還沒有實現如何執行HTTP請求,簡單來說就是還需要實現諸如do_GET(),do_POST()等函數,具體執行對應的HTTP命令。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.