2

我必須將參數傳遞給SimpleHTTPRequestHandler類,因此我使用類工廠創建瞭如下的自定義處理程序。將參數傳遞給SimpleHTTPRequestHandler

def RequestHandlerClass(application_path):  

    class CustomHandler(SimpleHTTPRequestHandler): 

    def __init__(self, request, client_address, server): 

     SimpleHTTPRequestHandler.__init__(self, request, client_address, server) 
     self._file_1_done = False 
     self._file_2_done = False 
     self._application_path = application_path 

    def _reset_flags(self): 

     self._file_1_done = False 
     self._file_2_done = False 

    def do_GET(self): 

     if (self.path == '/file1.qml'): 
      self._file_1_done = True 

     if (self.path == '/file2.qml'): 
      self._file_2_done = True 

     filepath = self._application_path + '/' + self.path # Error here 

     try: 
      f = open(filepath) 
      self.send_response(200) 
      self.end_headers() 
      self.wfile.write(f.read()) 
      f.close() 
     except IOError as e : 
      self.send_error(404,'File Not Found: %s' % self.path)  


     if (self._file_1_done and self._file_2_done): 
      self._reset_flags() 
      self.server.app_download_complete_event.set() 
    return CustomHandler 

這是使用自定義的處理

class PythonHtpServer(BaseHTTPServer.HTTPServer, threading.Thread): 

    def __init__(self, port, serve_path): 
    custom_request_handler_class = RequestHandlerClass(serve_path) 
    BaseHTTPServer.HTTPServer.__init__(self, ('0.0.0.0', port), custom_request_handler_class) 
    threading.Thread.__init__(self) 
    self.app_download_complete_event = threading.Event() 

    def run(self): 
    self.serve_forever() 

    def stop(self): 
    self.shutdown()  

我的httpserver,我開始與

http_server = PythonHtpServer(port = 8123, serve_path = '/application/main.qml') 

服務器啓動服務器,但我得到這個錯誤

AttributeError: CustomHandler instance has no attribute '_application_path' 

基本上,從錯誤,服務器確實開始,但我不知道爲什麼它不創建屬性(或者init沒有被調用)。請告訴我我哪裏錯了。任何幫助將受到歡迎。

回答

0

從概念上講,你寫這樣的事情(在此例如,application_path〜= var):

def createClass(var): 
    class MyClass: 
     def __init__(self): 
      self.var = var 
     def func(self): 
      # use var in some way 
      print (self.var) 
    # Return class definition 
    return MyClass 

因此類被寫入到保存變量var當創建MyClass實例。然而,在函數結束時,變量被破壞,由於該類只返回一個類定義而不是類實例MyClass實例不會獲得由當時的原始變量創建var被銷燬,因此變量var從來沒有真正被MyClass保存。

相反,你可以添加var作爲參數傳遞給MyClass.__init__功能,並創建一個生成器類來處理MyClass實例的創建,像這樣:

class MyClass: 
    def __init__(self, arg1, arg2, var): 
     (self.arg1, self.arg2, self.var) = (arg1, arg2, var) 
    # Insert other methods as usual 

class MyClassGenerator: 
    def __init__(self, var): 
     self.var = var 
    def make(self, arg1, arg2): 
     return MyClass(arg1, arg2, var) 
2

恕我直言,最簡單的方法就是讓_application_path靜態類的屬性。它僅在類聲明時申報,並可以通過類的實例透明地使用:

def RequestHandlerClass(application_path):  

    class CustomHandler(SimpleHTTPRequestHandler): 

    _application_path = application_path # static attribute 

    def __init__(self, request, client_address, server): 

     SimpleHTTPRequestHandler.__init__(self, request, client_address, server) 
     self._file_1_done = False 
     self._file_2_done = False 

    def _reset_flags(self): 
     ... 

這樣的自定義處理程序類的每個新實例將有機會獲得爲self._application_path應用程序的路徑。