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


Python ThreadingTCPServer.shutdown_request方法代码示例

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


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

示例1: ALPSTCPServer

# 需要导入模块: from SocketServer import ThreadingTCPServer [as 别名]
# 或者: from SocketServer.ThreadingTCPServer import shutdown_request [as 别名]
class ALPSTCPServer():
    bufsize = 1500
    @property
    def connections_list(self):
        return self.connections.keys()

    def __init__(self, ip, port, receiver_func=None, connection_event_func=None, background=False, thread_name="TCPServer Thread"):
        self.ip_string = ip
        self.port = port
        self.receiver_func_callback = receiver_func
        self.connection_event_callback = connection_event_func
        self.background = background
        self.connections = {}
        self.tcpserver = ThreadingTCPServer((self.ip_string, self.port), self.__request_handle_process)
        self.tcpserver.allow_reuse_address = True
        tcpsock_thread = ALPSThread(threadfunc=self.tcpserver.serve_forever, threadname=thread_name)
        tcpsock_thread.setDaemon(not self.background)
        tcpsock_thread.start()

    def __request_handle_process(self, sock_obj, client_address, server):
        self.connections[client_address] = sock_obj

        if callable(self.connection_event_callback):
            self.connection_event_callback(self, client_address, True)

        while True:
            try:
                data = sock_obj.recv(self.bufsize)
            except:
                data = ''

            if data:
                if callable(self.receiver_func_callback):
                    self.receiver_func_callback(self, client_address, data)
                    # print client_address
            else:
                self.connections.pop(client_address)
                if callable(self.connection_event_callback):
                    self.connection_event_callback(self, client_address, False)
                break

    def server_close(self):
        for sock_obj in self.connections.values():
            self.tcpserver.shutdown_request(sock_obj)
        self.tcpserver.shutdown()
        self.tcpserver.server_close()

    def send(self, client_address, message, error_except=True):
        if client_address not in self.connections.keys():
            ALPSDebug.alps_error('socket not exist error')
            return False
        connection = self.connections[client_address]

        if not error_except:
            return connection.sendall(message)

        try:
            connection.sendall(message)
        except Exception as e:
            ALPSDebug.alps_error(e, '\n', inspect.getframeinfo((inspect.currentframe().f_back)))
            return False
        return True

    def send_to_all_connections(self, message, error_except=True):
        if len(self.connections) == 0:
            return True
        for sock_obj in self.connections.values():
            if error_except:
                try:
                    sock_obj.sendall(message)
                except Exception as e:
                    ALPSDebug.alps_error(e, '\n', inspect.getframeinfo((inspect.currentframe().f_back)))
                    return False
            else:
                sock_obj.sendall(message)
        return True
开发者ID:peterwyj,项目名称:WSDT,代码行数:78,代码来源:ALPSTCP.py


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