Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass Parameter to SimpleHTTPRequestHandler

I have to pass a parameter to SimpleHTTPRequestHandler class, so I used class factory to create a custom handler as below.

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 

This is my httpserver using the custom handler

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()    

and I start the server with

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

The server starts, but I get this error

AttributeError: CustomHandler instance has no attribute '_application_path'

Basically, from the error, the server did start but I don't know why it is not creating the attributes ( or the init is not called ). Please tell me where I am going wrong. Any help would be welcome.

like image 335
user1429322 Avatar asked Oct 20 '25 13:10

user1429322


1 Answers

IMHO the simplest way is to make _application_path a static attribute of the class. It is declared only at class declaration time, and can be used transparently by instances of the class:

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):
        ...

That way every new instance of the custom handler class will have access to the application path as self._application_path.

like image 93
Serge Ballesta Avatar answered Oct 23 '25 01:10

Serge Ballesta



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!