Пример #1
0
    def __init__(self, pipe_conn, parent_handle):
        log.debug_info(u"_ListenPipe init()\n")

        threading.Thread.__init__(self)
        self.pipe_conn = pipe_conn
        self.parent_handle = parent_handle
        self.exit_listen_pipe_thread_flag = False
Пример #2
0
    def __init__(self, pipe_conn):
        """
        初始化
        """
        log.debug_info(u"BasePIPEChildControl init()\n")

        #保存通讯的管道句柄
        self.pipe_conn = pipe_conn
Пример #3
0
    def stop_listen_pipe_thread(self):
        """
        停止处理管道消息的子线程
        """
        log.debug_info(u"BasePIPEProcess stop_listen_pipe_thread\n")

        if self.listen_pipe_thread_handle:
            self.listen_pipe_thread_handle.exit_listen_pipe_thread()
Пример #4
0
    def __init__(self):
        """
        初始化
        """
        log.debug_info(u"BasePIPEParentControl init()\n")

        #初始化管道通讯句柄
        self.parent_conn, self.child_conn = multiprocessing.Pipe()
Пример #5
0
 def close_response_status_code(self):
     """
     关闭状态码响应模块
     """
     self.enable_response_status_code_flag = False
     log.debug_info(u"response status code status=%s" %
                    self.enable_response_status_code_flag)
     return True
Пример #6
0
 def open_response_status_code(self):
     """
     开启状态码响应模块
     """
     self.enable_response_status_code_flag = True
     log.debug_info(u"response status code status=%s" %
                    self.enable_response_status_code_flag)
     return True
Пример #7
0
    def start_listen_pipe_thread(self):
        """
        启动处理管道消息的子线程
        """
        log.debug_info(u"BasePIPEProcess start_listen_pipe_thread\n")

        if not self.listen_pipe_thread_handle:
            self.listen_pipe_thread_handle = _ListenPipe(self.pipe_conn, self)
            self.listen_pipe_thread_handle.setDaemon(True)
            self.listen_pipe_thread_handle.start()
Пример #8
0
    def _loginSucceeded(self, avatarInfo):
        """
        用户登录成功
        """
        avatarInterface, avatar, logout = avatarInfo
        log.debug_info(u"_loginSucceeded:fullname=%s" % avatar.fullname)

        #处理客户端具体数据请求
        self.process_dispath_request()

        #登出
        defer.maybeDeferred(logout).addBoth(self._logoutFinished)
Пример #9
0
    def dispatch_request_message_data(self, in_message_id, in_message_data):
        """
        将父进程管道连接下发的请求分发到具体处理模块
        """
        log.debug_info(
            u"BasePIPEChildControl dispatch_request_message_data message_id=%s"
            % in_message_id)

        #TODO
        self.response_run_methond_data(
            MESSAGE_ID_SET_RESPONSE_STATUS_CODE_NUMBER, MESSAGE_ID_ERROR,
            u'Dispatch request message function not interface is not implemented.'
        )
Пример #10
0
    def __init__(self, pipe_conn, in_address, in_port, in_home_workspace_dir):
        """
        初始化Twisted Http Server
        """
        log.debug_info(u"init")

        BasePIPEChildControl.__init__(self, pipe_conn)

        #开启和关闭用户权限验证标示
        self.enable_check_authorization_flag = False

        #开启和关闭状态码响应模块标示
        self.enable_response_status_code_flag = False

        #保存指定响应状态码
        self.response_status_code_data = 200

        #-----------------------------------------------------
        #add by wangjun 20131119
        #设置客户端认证模式Basic/Digest
        self.authorization_type = 'Basic'

        #设置客户端上传模式POST/PUT/BOTH
        self.client_upload_type_string = 'BOTH'
        #-----------------------------------------------------

        #保存用户账户信息
        self.users = {}
        self.passwords = {}

        self.http_factory = None

        #监听的端口号
        self.http_address = in_address
        self.http_port = int(in_port)

        #权限控制
        self.http_portal = portal.Portal(TestRealm(self.users))
        self.http_portal.registerChecker(PasswordDictChecker(self.passwords))

        #服务工厂
        #set portal
        self.http_factory = MyHttpFactory()
        self.http_factory.portal = self.http_portal

        #set site
        render_page_handle = MyRenderPage(in_home_workspace_dir)
        self.http_factory.site = render_page_handle

        #保存对象句柄到MyHttpFactory对象中
        self.http_factory.set_callback_app_handle(self)
Пример #11
0
    def __del__(self):
        """
        析构
        """
        log.debug_info(u"BasePIPEParentControl del()\n")

        try:
            log.debug_info(u"BasePIPEParentControl close pipe handle")

            self.parent_conn.close()
            self.child_conn.close()

        except Exception, e:
            pass
Пример #12
0
    def set_response_status_code(self, status_code):
        """
        设置状态码的值
        """
        if status_code.isdigit():

            if int(status_code) < 100 or int(status_code) > 600:
                return False

            self.response_status_code_data = int(status_code)
            log.debug_info(u"set status code=%d" %
                           self.response_status_code_data)
            return True
        else:
            return False
Пример #13
0
class _ListenPipe(threading.Thread):
    def __init__(self, pipe_conn, parent_handle):
        log.debug_info(u"_ListenPipe init()\n")

        threading.Thread.__init__(self)
        self.pipe_conn = pipe_conn
        self.parent_handle = parent_handle
        self.exit_listen_pipe_thread_flag = False

    def get_listen_thread_exit_flag(self):
        return self.exit_listen_pipe_thread_flag

    def run(self):
        """
        监听父进程发送的管道消息
        """
        while not self.exit_listen_pipe_thread_flag:

            log.debug_info(u"_ListenPipe runing ......")

            try:
                rc = self.recv_pipe_data(self.pipe_conn)
                if not rc:
                    self.exit_listen_pipe_thread_flag = True
                    break

                time.sleep(0.25)

            except Exception, e:
                error_info = u"recv_pipe_data error:%s" % e.message
                log.debug_info(error_info)
                self.exit_listen_pipe_thread()

        log.debug_info(u"_ListenPipe exit\n")
Пример #14
0
    def _loginFailed(self, failure):
        """
        用户登录失败
        """
        log.debug_info(u"_loginFailed: %s" % failure.getErrorMessage())
        rsp_string_data = str(failure.getErrorMessage())

        #构建常规响应消息头
        self._construct_response_header(http.UNAUTHORIZED,
                                        "text/html; charset=utf-8",
                                        str(len(rsp_string_data)))

        #未授权的, 构建Basic认证返回信息
        self.setHeader("WWW-Authenticate",
                       self._construct_auth_basic_response())
        self.write(MyChars.build_value_type_unicode_to_string(rsp_string_data))
        self.finish()
Пример #15
0
    def run(self):
        """
        监听父进程发送的管道消息
        """
        while not self.exit_listen_pipe_thread_flag:

            log.debug_info(u"_ListenPipe runing ......")

            try:
                rc = self.recv_pipe_data(self.pipe_conn)
                if not rc:
                    self.exit_listen_pipe_thread_flag = True
                    break

                time.sleep(0.25)

            except Exception, e:
                error_info = u"recv_pipe_data error:%s" % e.message
                log.debug_info(error_info)
                self.exit_listen_pipe_thread()
Пример #16
0
    def __init__(self, pipe_conn, in_serve_listen_address,
                 in_serve_listen_port, in_home_workspace_dir):

        log.debug_info(u"BasePIPEProcess init()\n")

        #初始化基类
        multiprocessing.Process.__init__(self)

        #服务对象句柄
        self.serve_modle_object_handle = None

        #监听管道消息线程句柄
        self.listen_pipe_thread_handle = None

        #与父进程通信管道
        self.pipe_conn = pipe_conn

        #监听地址
        self.serve_listen_address = in_serve_listen_address
        self.serve_listen_port = in_serve_listen_port
        self.home_workspace_dir = in_home_workspace_dir
Пример #17
0
    def dispatch_request_message_data(self, in_message_id, in_message_data):
        """
        分发消息
        """
        log.debug_info(u"BasePIPEProcess dispatch_request_message_data")

        if self.serve_modle_object_handle:
            if hasattr(self.serve_modle_object_handle,
                       'dispatch_request_message_data'):

                #下发具体消息请求
                self.serve_modle_object_handle.dispatch_request_message_data(
                    in_message_id, in_message_data)

            else:
                #HTTP服务器对象没有找到接收消息请求函数入口
                response_data = STRING_DISPATH_REQUEST_MESSAGE_HANDLE_NOT_INIT[
                    local_lang_type]
                log.debug_info(response_data)
                self.pipe_conn.send(
                    [RESPONSE, in_message_id, MESSAGE_ID_ERROR, response_data])
        else:

            #HTTP服务器对象没有初始化
            response_data = STRING_DISPATH_REQUEST_MESSAGE_HANDLE_NOT_FOUND_RECV_ACTION_FUNCTION[
                local_lang_type]
            log.debug_info(response_data)
            self.pipe_conn.send(
                [RESPONSE, in_message_id, MESSAGE_ID_ERROR, response_data])

        #退出消息监听线程
        if MESSAGE_ID_QUIT == in_message_id:
            self.stop_listen_pipe_thread()
Пример #18
0
    def _get_client_upload_type_string(self):
        """
        获取客户端上传模式POST/PUT/BOTH
        """
        #检查用户回调模块接口是否开放
        if CALLBACK_HANDLE_NOTINIT == self.channel.factory.callback_app_hanlde_init_status:
            return False, None

        #获取TWHttpServer对象句柄
        temp_hanlde = self.channel.factory.callback_app_hanlde
        if not temp_hanlde:
            rsp_string_data = MyChars.get_string_value(
                MyChars.STRING_FACTORY_CALLBACK_APP_HANDLE_NONETYPE)
            log.debug_info(rsp_string_data)
            return False, None

        #检查接口是否存在
        if not hasattr(temp_hanlde, "get_client_upload_type"):
            rsp_string_data = MyChars.get_string_value(
                MyChars.STRING_FACTORY_CALLBACK_APP_NOTDEFINE_GET_AUTH_TYPE)
            log.debug_info(rsp_string_data)
            return False, None

        #获取客户端上传模式POST/PUT/BOTH
        temp_client_upload_type = temp_hanlde.get_client_upload_type()
        log.debug_info(u"get_client_upload_type:%s" % temp_client_upload_type)

        #返回客户端上传模式POST/PUT/BOTH
        return True, temp_client_upload_type
Пример #19
0
    def _get_digest_authorization_uername_register_password(self, username):
        """
        获取Digest认证中username注册的密码数据
        """
        #检查用户回调模块接口是否开放
        if CALLBACK_HANDLE_NOTINIT == self.channel.factory.callback_app_hanlde_init_status:
            return False, None

        #获取TWHttpServer对象句柄
        temp_hanlde = self.channel.factory.callback_app_hanlde
        if not temp_hanlde:
            rsp_string_data = MyChars.get_string_value(
                MyChars.STRING_FACTORY_CALLBACK_APP_HANDLE_NONETYPE)
            log.debug_info(rsp_string_data)
            return False, None

        #检查接口是否存在
        if not hasattr(temp_hanlde, "get_client_register_password"):
            rsp_string_data = MyChars.get_string_value(
                MyChars.STRING_FACTORY_CALLBACK_APP_NOTDEFINE_GET_AUTH_TYPE)
            log.debug_info(rsp_string_data)
            return False, None

        #获取客户端上传模式POST/PUT/BOTH
        rc_status, temp_client_register_password = temp_hanlde.get_client_register_password(
            username)
        log.debug_info(u"get_client_register_password:%s" %
                       temp_client_register_password)

        #返回客户端上传模式POST/PUT/BOTH
        return rc_status, temp_client_register_password
Пример #20
0
    def _get_check_authorization_flag(self):
        """
        获取用户权限验证是否打开状态
        """
        #检查用户回调模块接口是否开放
        if CALLBACK_HANDLE_NOTINIT == self.channel.factory.callback_app_hanlde_init_status:
            return False

        #获取TWHttpServer对象句柄
        temp_hanlde = self.channel.factory.callback_app_hanlde
        if not temp_hanlde:
            rsp_string_data = MyChars.get_string_value(
                MyChars.STRING_FACTORY_CALLBACK_APP_HANDLE_NONETYPE)
            log.debug_info(rsp_string_data)
            return False

        #检查句柄是否有获取用户权限验证模块接口
        if not hasattr(temp_hanlde, "get_enable_check_authorization_flag"):
            rsp_string_data = MyChars.get_string_value(
                MyChars.
                STRING_FACTORY_CALLBACK_APP_NOTDEFINE_GET_CHECKAUTH_MOTHOD)
            log.debug_info(rsp_string_data)
            return False

        #获取用户权限验证是否打开
        temp_enable_status = temp_hanlde.get_enable_check_authorization_flag()
        log.debug_info(u"get_enable_check_authorization_flag:%s" %
                       temp_enable_status)

        #返回状态标志
        return temp_enable_status
Пример #21
0
    def process_authorization_basic(self):
        """
        basic权限验证
        """

        #读取用户账户信息
        client_username = self.getUser()
        client_password = self.getPassword()

        if client_username:
            log.debug_info(u"%s" % client_username)
            log.debug_info(u"%s" % client_password)

            #验证用户权限,如果有权限则处理客户端具体数据请求
            self.basic_authorization_handle_login(client_username,
                                                  client_password)

        else:
            #未授权的
            rsp_string_data = MyChars.get_string_value(
                MyChars.STRING_REQUEST_PROCESS_401_ERROR)

            #获取返回数据长度
            rsp_string_data_length = len(
                MyChars.build_value_type_unicode_to_string(rsp_string_data))

            #设置响应消息常规头数据
            self._construct_response_header(http.UNAUTHORIZED,
                                            "text/html; charset=utf-8",
                                            str(rsp_string_data_length))

            #未授权的, 构建Basic认证返回信息
            self.setHeader("WWW-Authenticate",
                           self._construct_auth_basic_response())

            self.write(
                MyChars.build_value_type_unicode_to_string(rsp_string_data))
            self.finish()
Пример #22
0
    def process(self):
        """
        处理来自客户端的连接请求
        """
        try:

            #获取客户端HTTP版本信息
            http_proto_version = self.clientproto
            log.debug_info(http_proto_version)

            #解析Connection头数据
            connection_type = self.getHeader('Connection')
            if 'HTTP/1.1' == http_proto_version:
                #主动关闭连接是,在响应头中设置close连接属性
                if (connection_type and CONNECTION_KEEP_CLOSE.lower()
                        == connection_type.lower()):
                    self.setHeader('Connection', CONNECTION_KEEP_CLOSE)

            #获取用户权限验证是否打开
            temp_check_authorization_flag = self._get_check_authorization_flag(
            )
            self.__enable_check_authorization_status = temp_check_authorization_flag

            if self.__enable_check_authorization_status:

                #权限认证
                self.process_authorization()

            else:

                #处理客户端具体数据请求
                self.process_dispath_request()

        except Exception, e:
            #服务器内部错误
            self._internal_server_error(e)
Пример #23
0
    def _get_response_status_code_value(self):
        """
        获取响应指定状态码功能模块是否打开状态
        """

        #检查用户回调模块接口是否开放
        if CALLBACK_HANDLE_NOTINIT == self.channel.factory.callback_app_hanlde_init_status:
            return 0, None

        #获取TWHttpServer对象句柄
        temp_hanlde = self.channel.factory.callback_app_hanlde
        if not temp_hanlde:
            rsp_string_data = MyChars.get_string_value(
                MyChars.STRING_FACTORY_CALLBACK_APP_HANDLE_NONETYPE)
            log.debug_info(rsp_string_data)
            return -1, rsp_string_data

        #检查句柄是否有获取状态码功能模块接口
        if not (hasattr(temp_hanlde, "get_enable_response_status_code_flag")
                and hasattr(temp_hanlde,
                            "get_enable_response_status_code_flag")):
            rsp_string_data = MyChars.get_string_value(
                MyChars.
                STRING_FACTORY_CALLBACK_APP_NOTDEFINE_GET_RESPONSE_STATUS_CODE_MOTHOD
            )
            log.debug_info(rsp_string_data)
            return -1, rsp_string_data

        #获取响应指定状态码功能模块是否打开
        temp_enable_status = temp_hanlde.get_enable_response_status_code_flag()
        log.debug_info(u"get_enable_response_status_code_flag:%s" %
                       temp_enable_status)

        if not temp_enable_status:
            #响应指定状态码功能模块是关闭的
            return 0, None

        else:
            #获取指定状态码的值
            tmp_response_status_code_number = temp_hanlde.get_response_status_code(
            )
            log.debug_info(u"get_response_status_code:%d" %
                           tmp_response_status_code_number)

            #返回指定状态码的值
            return 1, tmp_response_status_code_number
Пример #24
0
    def response_run_methond_data(self, in_message_id, in_response_status,
                                  in_message_data):
        """
        给父进程管道连接回响应信息
        """
        log.debug_info(u"BasePIPEChildControl response_run_methond_data start")

        if self.pipe_conn:
            log.debug_info(u"Pipe send response data success")

            #发送正常处理数据结果
            self.pipe_conn.send(
                [RESPONSE, in_message_id, in_response_status, in_message_data])
            return True

        else:
            #管道句柄数据为空,打印错误信息
            log.debug_info(u"Pipe send response data fail")
            return False
Пример #25
0
    def stop_httpservet(self):
        """
        停止Twisted Http Server
        """
        try:
            log.debug_info(u"reactor.stop start")

            #停止SERVER监听网络
            reactor.callFromThread(reactor.stop)

            rsp_string_data = MyChars.get_string_value(
                MyChars.STRING_HTTPSERVER_REACTOR_STOP_SUC)
            log.debug_info(rsp_string_data)
            return True, rsp_string_data

        except Exception, e:  #stop_httpservet method
            #log.debug_info(e)
            rsp_string_data = MyChars.get_string_value(
                MyChars.STRING_HTTPSERVER_REACTOR_STOP_ERROR)
            log.debug_info(rsp_string_data)
            return False, rsp_string_data
Пример #26
0
    def _internal_server_error(self, except_e):
        """
        服务器内部错误响应
        """
        log.debug_info(type(except_e))
        log.debug_info(except_e)

        #服务器内部错误
        rsp_string_data = MyChars.get_string_value(
            MyChars.STRING_REQUEST_PROCESS_500_ERROR)
        log.debug_info(rsp_string_data)

        #获取返回数据长度
        rsp_string_data_length = len(
            MyChars.build_value_type_unicode_to_string(rsp_string_data))

        #设置响应消息常规头数据
        self._construct_response_header(http.INTERNAL_SERVER_ERROR,
                                        "text/html; charset=utf-8",
                                        str(rsp_string_data_length))
        self.write(MyChars.build_value_type_unicode_to_string(rsp_string_data))
        self.finish()
Пример #27
0
    def recv_pipe_data(self, conn):
        """
        收取消息
        """
        #判断通讯管道句柄
        if not conn:
            return False

        #收取pipe中的消息数据
        log.debug_info(u"_ListenPipe recv_pipe_data begin...")

        try:
            #接收管道中的消息
            r_data = conn.recv()
            log.debug_info(r_data)

        except Exception, e:
            error_info = u"recv_pipe_data error:%s" % e.message
            log.debug_info(error_info)
            conn.send(
                [RESPONSE, MESSAGE_ID_UNKNOWN, MESSAGE_ID_ERROR, error_info])

            #抛出异常到调用的函数接口
            raise RuntimeError(error_info)
Пример #28
0
    def exit_listen_pipe_thread(self):
        log.debug_info(u"_ListenPipe exit_listen\n")

        self.exit_listen_pipe_thread_flag = True
Пример #29
0
    def recv_child_pipe_response_message_data(self):
        """
        处理管道中的消息
        """
        log.debug_info(
            u"BasePIPEParentControl recv_child_pipe_response_message_data()\n")

        if not self.parent_conn:
            log.debug_info(u"%s" % RECV_PIPE_HANDLE_NOT_INIT)
            error_info = u"HttpServer process recv pipe handle not init."
            return False, RECV_PIPE_HANDLE_NOT_INIT, error_info

        log.debug_info(u"_recv_message_response_data running...")

        try:
            #接收管道中的消息
            r_data = self.parent_conn.recv()

        except Exception, e:
            error_info = u"recv_pipe_data error:%s" % e.message
            log.debug_info(error_info)

            if message_id != MESSAGE_ID_QUIT:
                log.debug_info(u"%s" % RESPONSE_REQUEST_RUN_FAIL)
                return False, RESPONSE_REQUEST_RUN_FAIL, error_info

            else:
                log.debug_info(u"%s" % RESPONSE_REQUEST_QUIT_PROCESS_FAIL)
                return False, RESPONSE_REQUEST_QUIT_PROCESS_FAIL, error_info
Пример #30
0
 def __del__(self):
     """
     析构
     """
     log.debug_info(u"BasePIPEChildControl del()\n")