Exemplo n.º 1
0
class FileSearchServer(Thread):
	""" Thread which answers to file/tag queries sent through unix socket. """
	def __init__(self, pname='SET_ME_PLEASE'):

		Thread.__init__(self)

		self.name = "%s/%s" % (
			pname, str(self.__class__).rsplit('.', 1)[1].split("'")[0])

		# old socket from a crashed daemon ?
		# remove it, the ThreadingUnixStreamServer will create it.
		#if os.path.exists(socket_path): os.unlink(socket_path)

		self._stop_event = Event()
		self.server     = ThreadingTCPServer(('127.0.0.1', searcher_port), FileSearchRequestHandler)
		self.server.allow_reuse_address = True

		# TODO: the socket is set to non-blocking to be able to gracefully terminate the thread,
		# but this could lead to CPU hogging problems. VERIFY THIS !!
		self.server.socket.setblocking(False)
	def run(self):
		logging.progress("%s: thread running." % (self.getName()))
		#os.chmod(socket_path, stat.S_IRUSR|stat.S_IWUSR|stat.S_IRGRP|stat.S_IWGRP|stat.S_IROTH|stat.S_IWOTH)
		while not self._stop_event.isSet():
			self.server.handle_request()
			time.sleep(0.01)
		logging.progress("%s: thread ended." % (self.getName()))
	def stop(self):
		if not self._stop_event.isSet():
			logging.progress("%s: stopping thread." % (self.getName()))
			self._stop_event.set()
			self.server.socket.close()
			self.server.server_close()
			if os.path.exists(socket_path):
				os.unlink(socket_path)
Exemplo n.º 2
0
class XXEWebServer(Thread):

    def __init__(self, host='0.0.0.0', port=8000, HandlerClass=XXEHandler):
        Thread.__init__(self)
        self._stop = Event()
        self.host = host
        self.port = port
        self.server_address = (self.host, self.port)
        self.HandlerClass = HandlerClass

        if self.HandlerClass is None:
            self.HandlerClass = SimpleHTTPRequestHandler
            self.HandlerClass.protocol_version = "HTTP/1.0"
            self.httpd = HTTPServer(self.server_address, self.HandlerClass)
        else:
            self.httpd = ThreadingTCPServer(self.server_address, self.HandlerClass)

    def stop(self):
        self._stop.set()

    def stopped(self):
        return self._stop.isSet()

    def run(self):
        sa = self.httpd.socket.getsockname()
        print "Serving HTTP on ", sa[0], " port ", sa[1]
        #self.httpd.serve_forever()
        while not self.stopped():
            if exiting: return
            self.httpd.handle_request()
Exemplo n.º 3
0
 def handle_request(self):
     print " handle request "
     handler = ThreadingTCPServer.handle_request(self)
     handler.set_handlers(self.handleWiFlyMessage,
                          self.handleBrowserMessage)
     clients.append(handler)
     return handler
class HttpMonitor(Monitor):
    """
    Monitor incoming documents on a HTTP endpoint.

    For messages received via HTTP GET it uses the the path from URL path after host address as a 'str' type input.
    For messages received via HTTP POST it expects the content body to be JSON.

    Sockets:
        output     (*)       : Document received on the HTTP endpoint.

    Config:
        host              = "localhost"
        port              = 4000
        max_length        = 1024*1024     : Max length of incoming data via POST, in bytes.
    """

    ThreadingTCPServer.allow_reuse_address = True  # OBS: Class level setting.

    def __init__(self, hook=None, **kwargs):
        super(HttpMonitor, self).__init__(**kwargs)
        self.output = self.create_socket("output", None, "Document received on the HTTP endpoint.")
        self.config.set_default(host="localhost", port=4000)

        self.config.set_default(
            host       = "localhost",
            port       = 4000,
            max_length = 1024*1024  # 1 MB
        )

        self.hook = hook

    def on_open(self):
        self.log.info("Starting HTTP listener on %s:%d" % (self.config.host, self.config.port))
        self._server = ThreadingTCPServer((self.config.host, self.config.port), _ServerHandlerClass, bind_and_activate=True)
        self._server.owner = self
        self._server.timeout = 1.0  # Max 1 second blocking in _server.handle_request()

    def on_close(self):
        self.log.info("Closing HTTP listener.")
        self._server.server_close()
        self._server = None

    def _incoming_WITH_DATA(self, request_handler, verb, path, data):
        if self.suspended:
            return "Server suspended; incoming data ignored."

        if verb == "GET":
            self.total += 1
            self.count += 1
            # For GET, use path as document
            if self.output.has_output:
                self.output.send(path[1:])

        data_obj = None
        if data:
            if request_handler.headers.gettype() == "application/json":
                try:
                    data_obj = json.loads(data)
                except Exception as e:
                    self.doclog.error("Failed to parse incoming data as JSON: " + e.message)
                    return "Failed to parse data as JSON."
            else:
                data_obj = data

        if verb != "GET":
            self.total += 1
            self.count += 1
            if self.output.has_output:
                # For non-GET, use data as document
                self.output.send(data_obj)

        return self._call_hook(request_handler, verb, path, data_obj)

    def _call_hook(self, request_handler, http_verb, path, data):
        if self.hook:
            try:
                return self.hook(request_handler, http_verb, path, data)
            except Exception as e:
                self.log.exception("Failed to call hook: %s: %s" % (e.__class__.__name__, e))


    def on_startup(self):
        self.total = 0
        self.count = 0

    def on_tick(self):
        self._server.handle_request()
Exemplo n.º 5
0
 def handle_request(self):
     print " handle request "
     handler = ThreadingTCPServer.handle_request(self)
     handler.set_handlers(self.handleWiFlyMessage, self.handleBrowserMessage)
     return handler