Subversion Repositories basico

Rev

Rev 97 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

#!/usr/bin/python
# -*- coding: utf-8 -*-
# File: webserver.py
# Author: Tomás Vírseda
# License: GPL v3
# Description: Simple and tiny WebServer service for Basico

import os
import time
import threading
from http.server import HTTPServer, SimpleHTTPRequestHandler

from .service import Service
from .env import LPATH, GPATH, SEP

# FIXME: make PORT dynamic via configuration
PORT = 8763 # Custom Port. Could be any above 1024.
WEBROOT = LPATH['WWW']

class LoggingRequestHandler(SimpleHTTPRequestHandler):
    '''Custom class based on SimpleHTTPRequestHandler which disables
    logging
    '''

    def log_message(self, format, *args):
        return


class WebServer(Service):
    def initialize(self):
        pass


    def run(self):
        os.chdir(WEBROOT)
        self.webserver = HTTPServer(('localhost', PORT), LoggingRequestHandler)
        self.thread = threading.Thread(target = self.webserver.serve_forever)
        self.thread.daemon = True
        self.thread.start()
        self.log.debug("Basico WebServer Document Root: %s" % WEBROOT)
        self.log.debug('Basico WebServer listening on port: {}'.format(self.webserver.server_port))


    def halt(self):
        try:
            self.webserver.shutdown()
            self.thread.join()
        except:
            pass



    def finalize(self):
        self.halt()