本文整理汇总了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)
示例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()
示例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