当前位置: 首页>>代码示例>>Python>>正文


Python ThreadingTCPServer.handle_request方法代码示例

本文整理汇总了Python中SocketServer.ThreadingTCPServer.handle_request方法的典型用法代码示例。如果您正苦于以下问题:Python ThreadingTCPServer.handle_request方法的具体用法?Python ThreadingTCPServer.handle_request怎么用?Python ThreadingTCPServer.handle_request使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在SocketServer.ThreadingTCPServer的用法示例。


在下文中一共展示了ThreadingTCPServer.handle_request方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: FileSearchServer

# 需要导入模块: from SocketServer import ThreadingTCPServer [as 别名]
# 或者: from SocketServer.ThreadingTCPServer import handle_request [as 别名]
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)
开发者ID:Licorn,项目名称:licorn,代码行数:37,代码来源:searcher.py

示例2: HttpMonitor

# 需要导入模块: from SocketServer import ThreadingTCPServer [as 别名]
# 或者: from SocketServer.ThreadingTCPServer import handle_request [as 别名]
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()
开发者ID:comperiosearch,项目名称:elasticsearch-eslib,代码行数:89,代码来源:HttpMonitor.py

示例3: handle_request

# 需要导入模块: from SocketServer import ThreadingTCPServer [as 别名]
# 或者: from SocketServer.ThreadingTCPServer import handle_request [as 别名]
 def handle_request(self):
     print " handle request "
     handler = ThreadingTCPServer.handle_request(self)
     handler.set_handlers(self.handleWiFlyMessage, self.handleBrowserMessage)
     return handler
开发者ID:flxhbck,项目名称:MarioKard,代码行数:7,代码来源:app.py


注:本文中的SocketServer.ThreadingTCPServer.handle_request方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。