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
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
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
def wirte_result_data(self, source, request): """ 填充数据内容,并发送 """ try: if isinstance(source, file): request.write( MyChars.build_value_type_unicode_to_string(source.read())) else: global g_import_stringio_type type_string = str(type(source)) #print type_string if type_string.find(".StringO"): request.write( MyChars.build_value_type_unicode_to_string( source.getvalue())) else: rsp_string_data = MyChars.get_string_value( MyChars.STRING_REQUEST_RENDER_ERROR_READ_VALUE_ERROR) request.write( MyChars.build_value_type_unicode_to_string( rsp_string_data)) except Exception, e: rsp_string_data = MyChars.get_string_value( MyChars.STRING_REQUEST_RENDER_ERROR_READ_VALUE_ERROR) request.write( MyChars.build_value_type_unicode_to_string(rsp_string_data))
def requestAvatarId(self, credentials): username = credentials.username if self.passwords.has_key(username): if credentials.password == self.passwords[username]: return defer.succeed(username) else: rsp_string_data=MyChars.get_string_value(MyChars.STRING_REQUEST_CRED_BAD_PASSWORD) return defer.fail(credError.UnauthorizedLogin(rsp_string_data)) else: rsp_string_data=MyChars.get_string_value(MyChars.STRING_REQUEST_CRED_NO_SUCH_USER) return defer.fail(credError.UnauthorizedLogin(rsp_string_data))
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
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
def start_httpservet(self): """ 启动Twisted Http Server """ if not self.http_factory: rsp_string_data = MyChars.get_string_value( MyChars.STRING_HTTPSERVER_FACTORY_OBJECT_HANDLE_NOT_INIT) log.debug_info(rsp_string_data) raise RuntimeError(rsp_string_data) #将异常抛到上层接口 log.debug_info(u"start_httpservet start") try: log.debug_info(u"reactor.listenTCP start") #设置监听对象 reactor.listenTCP(self.http_port, self.http_factory, interface=self.http_address) except Exception, e: #reactor.listenTCP mthod #add by wangjun 20131127 #停止SERVER监听网络 reactor.stop if isinstance(e, CannotListenError): rsp_string_data = MyChars.get_string_value( MyChars.STRING_HTTPSERVER_REACTOR_CANNOT_LISTEN_THIS_PORT ) % (self.http_address, self.http_port) else: rsp_string_data = MyChars.get_string_value( MyChars.STRING_HTTPSERVER_REACTOR_LISTEN_TCP_ERROR) log.debug_info("-------------------------------------") log.debug_info(rsp_string_data) log.debug_info("-------------------------------------") raise RuntimeError(rsp_string_data) #将异常抛到上层接口
def requestAvatar(self, avatarId, mind, *interfaces): if INamedUserAvatar in interfaces: fullname = self.users[avatarId] logout = lambda: None return (INamedUserAvatar, NamedUserAvatar(avatarId, fullname), logout) else: rsp_string_data=MyChars.get_string_value(MyChars.STRING_REQUEST_CRED_NOTDEFINE_INTERFACE) raise KeyError(rsp_string_data)
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()
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()
def deal_put_data(self, request): """ PUT方法数据处理方法 """ #获取content-length头 remainbytes = int(request.getHeader('content-length')) #获取文件名字和存放路径 self.path = request.path path = self.translate_path(self.path, request) osType = platform.system() try: if osType == "Linux": fn = os.path.join(path, fn[0].decode('gbk').encode('utf-8')) else: fn = path except Exception, e: rsp_string_data = MyChars.get_string_value( MyChars.STRING_REQUEST_RENDER_ERROR_FILENAME_ERROR) return (False, rsp_string_data)
def process_authorization(self): """ 进行权限验证 """ #获取客户端请求认证头数据 authorization_data = self.getHeader('authorization') #获取服务器设置的默认认证模式 rc_status, temp_cfg_client_authorization_type = self._get_client_authorization_string( ) #识别认证类型状态, # ---- 0是初始化状态,表示没有找到认证消息头, # ---- -1表示不识别类型, # ---- -2表示识别类型不匹配, # ---- 1表示识别类型成功 temp_check_authorization_type_state = 0 if authorization_data: #读取客户端请求消息认证头认证类型数据 temp_request_authorization_type = None if 0 == authorization_data.find('Digest'): temp_request_authorization_type = 'Digest' elif 0 == authorization_data.find('Basic'): temp_request_authorization_type = 'Basic' if not temp_request_authorization_type: #不识别认证类型 temp_check_authorization_type_state = -1 else: #配置的认证类型与客户端请求认证类型相同 if (temp_request_authorization_type == temp_cfg_client_authorization_type): #识别类型成功 temp_check_authorization_type_state = 1 #'Digest'认证 if ('Digest' == temp_request_authorization_type): self.process_authorization_digest(authorization_data) #Basic认证 else: self.process_authorization_basic() return else: #不匹配认证类型 temp_check_authorization_type_state = -2 #认证不成功,构造认证失败的消息给客户端响应 if 1 != temp_check_authorization_type_state: rsp_string_data = '401 Unauthorized' #未找到验证头数据 if 0 == temp_check_authorization_type_state: rsp_string_data = MyChars.get_string_value( MyChars.STRING_REQUEST_PROCESS_401_ERROR) #不识别认证类型 elif -1 == temp_check_authorization_type_state: rsp_string_data = MyChars.get_string_value( MyChars.STRING_REQUEST_PROCESS_DOES_NOT_RECOGNIZE_AUTH_TYPE ) #不匹配认证类型 else: rsp_string_data = MyChars.get_string_value( MyChars. STRING_REQUEST_PROCESS_AUTH_TYPE_IS_NOT_CONFIGURED_MODE) #回响应数据给客户端请求 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.UNAUTHORIZED, "text/html; charset=utf-8", str(rsp_string_data_length)) #未授权的, 构建Basic/Digest认证返回信息, 默认为Basic if 'Digest' == temp_cfg_client_authorization_type: self.setHeader("WWW-Authenticate", self._construct_auth_digest_response()) else: self.setHeader("WWW-Authenticate", self._construct_auth_basic_response()) self.write( MyChars.build_value_type_unicode_to_string(rsp_string_data)) self.finish() return
def process_dispath_request(self): """ 处理客户端具体数据请求 """ #调用响应指定状态码功能模块 rc_run_method_flag = self.process_response_status_code() if rc_run_method_flag: return #处理上传类型 rc_status, temp_cfg_client_upload_type = self._get_client_upload_type_string( ) if (not rc_status or ("GET" != self.method and "BOTH" != temp_cfg_client_upload_type and self.method != temp_cfg_client_upload_type)): #上传类型错误 rsp_string_data = MyChars.get_string_value( MyChars. STRING_REQUEST_PROCESS_UPLOAD_TYPE_IS_NOT_CONFIGURED_MODE) #获取返回数据长度 rsp_string_data_length = len( MyChars.build_value_type_unicode_to_string(rsp_string_data)) #设置响应消息常规头数据 self._construct_response_header(http.NOT_ALLOWED, "text/html; charset=utf-8", str(rsp_string_data_length)) self.write( MyChars.build_value_type_unicode_to_string(rsp_string_data)) self.finish() return #处理消息正常流程 self.site = self.channel.factory.site if self.method == "PUT": self.site.render_PUT(self) elif self.method == "POST": self.site.render_POST(self) elif self.method == "GET": self.site.render_GET(self) else: #方法不支持 rsp_string_data = MyChars.get_string_value( MyChars.STRING_REQUEST_PROCESS_405_ERROR) #获取返回数据长度 rsp_string_data_length = len( MyChars.build_value_type_unicode_to_string(rsp_string_data)) #设置响应消息常规头数据 self._construct_response_header(http.NOT_ALLOWED, "text/html; charset=utf-8", str(rsp_string_data_length)) self.write( MyChars.build_value_type_unicode_to_string(rsp_string_data)) self.finish()
def dispatch_request_message_data(self, in_message_id, in_message_data): """ 重载基类接口,将父进程管道连接下发的请求分发到具体处理模块 """ log.debug_info(u"dispatch_request_message_data message_id=%s" % in_message_id) try: #退出消息 if MESSAGE_ID_QUIT == in_message_id: rc_status, rc_data = self.stop_httpservet() response_status = MESSAGE_ID_SUC response_data = MyChars.get_string_value( MyChars.STRING_HTTPSERVER_STOP_SUC) log.debug_info(response_data) if not rc_status: response_status = MESSAGE_ID_ERROR response_data = MyChars.get_string_value( MyChars.STRING_HTTPSERVER_STOP_FAIL) % rc_data log.debug_info(response_data) self.response_run_methond_data(MESSAGE_ID_QUIT, response_status, response_data) #创建账户消息 elif MESSAGE_ID_REGISTER_USER_ACCOUNT == in_message_id: rc_status = self.register_user_account(*in_message_data) response_status = MESSAGE_ID_SUC response_data = MyChars.get_string_value( MyChars.STRING_HTTPSERVER_REGISTER_USER_ACCOUNT_SUC) log.debug_info(response_data) if not rc_status: response_status = MESSAGE_ID_ERROR response_data = MyChars.get_string_value( MyChars.STRING_HTTPSERVER_REGISTER_USER_ACCOUNT_FAIL) log.debug_info(response_data) self.response_run_methond_data( MESSAGE_ID_REGISTER_USER_ACCOUNT, response_status, response_data) #注销账户消息 elif MESSAGE_ID_UNREGISTER_USER_ACCOUNT == in_message_id: rc_status = self.unregister_user_account(in_message_data) response_status = MESSAGE_ID_SUC response_data = MyChars.get_string_value( MyChars.STRING_HTTPSERVER_UNREGISTER_USER_ACCOUNT_SUC) log.debug_info(response_data) if not rc_status: #response_status=MESSAGE_ID_ERROR #response_data=MyChars.get_string_value(MyChars.STRING_HTTPSERVER_UNREGISTER_USER_ACCOUNT_FAIL) response_status = MESSAGE_ID_SUC response_data = MyChars.get_string_value( MyChars. STRING_HTTPSERVER_UNREGISTER_NOTFOUND_USER_ACCOUNT) log.debug_info(response_data) self.response_run_methond_data( MESSAGE_ID_UNREGISTER_USER_ACCOUNT, response_status, response_data) #打开用户权限验证模块 elif MESSAGE_ID_OPEN_CHECK_AUTHORIZATION == in_message_id: rc_status = self.open_check_authorization() response_status = MESSAGE_ID_SUC response_data = MyChars.get_string_value( MyChars. STRING_HTTPSERVER_OPEN_CHECK_AUTHORIZATION_MODULE_SUC) log.debug_info(response_data) if not rc_status: response_status = MESSAGE_ID_ERROR response_data = MyChars.get_string_value( MyChars. STRING_HTTPSERVER_OPEN_CHECK_AUTHORIZATION_MODULE_FALSE ) log.debug_info(response_data) self.response_run_methond_data( MESSAGE_ID_OPEN_CHECK_AUTHORIZATION, response_status, response_data) #关闭用户权限验证模块 elif MESSAGE_ID_CLOSE_CHECK_AUTHORIZATION == in_message_id: rc_status = self.close_check_authorization() response_status = MESSAGE_ID_SUC response_data = MyChars.get_string_value( MyChars. STRING_HTTPSERVER_CLOSE_CHECK_AUTHORIZATION_MODULE_SUC) log.debug_info(response_data) if not rc_status: response_status = MESSAGE_ID_ERROR response_data = MyChars.get_string_value( MyChars. STRING_HTTPSERVER_CLOSE_CHECK_AUTHORIZATION_MODULE_FAIL ) log.debug_info(response_data) self.response_run_methond_data( MESSAGE_ID_CLOSE_CHECK_AUTHORIZATION, response_status, response_data) #打开响应状态码模块 elif MESSAGE_ID_OPEN_RESPONSE_STATUS_CODE == in_message_id: rc_status = self.open_response_status_code() response_status = MESSAGE_ID_SUC response_data = MyChars.get_string_value( MyChars. STRING_HTTPSERVER_OPEN_RESPONSE_STATUS_CODE_MODULE_SUC) log.debug_info(response_data) if not rc_status: response_status = MESSAGE_ID_ERROR response_data = MyChars.get_string_value( MyChars. STRING_HTTPSERVER_OPEN_RESPONSE_STATUS_CODE_MODULE_FAIL ) log.debug_info(response_data) self.response_run_methond_data( MESSAGE_ID_OPEN_RESPONSE_STATUS_CODE, response_status, response_data) #关闭响应状态码模块 elif MESSAGE_ID_CLOSE_RESPONSE_STATUS_CODE == in_message_id: rc_status = self.close_response_status_code() response_status = MESSAGE_ID_SUC response_data = MyChars.get_string_value( MyChars. STRING_HTTPSERVER_CLOSE_RESPONSE_STATUS_CODE_MODULE_SUC) log.debug_info(response_data) if not rc_status: response_status = MESSAGE_ID_ERROR response_data = MyChars.get_string_value( MyChars. STRING_HTTPSERVER_CLOSE_RESPONSE_STATUS_CODE_MODULE_FAIL ) log.debug_info(response_data) self.response_run_methond_data( MESSAGE_ID_CLOSE_RESPONSE_STATUS_CODE, response_status, response_data) #设置状态码的值 elif MESSAGE_ID_SET_RESPONSE_STATUS_CODE_NUMBER == in_message_id: rc_status = self.set_response_status_code(in_message_data) response_status = MESSAGE_ID_SUC response_data = MyChars.get_string_value( MyChars.STRING_HTTPSERVER_SET_RESPONSE_STATUS_CODE_SUC) log.debug_info(response_data) if not rc_status: response_status = MESSAGE_ID_ERROR response_data = MyChars.get_string_value( MyChars.STRING_HTTPSERVER_SET_RESPONSE_STATUS_CODE_FAIL ) log.debug_info(response_data) self.response_run_methond_data( MESSAGE_ID_SET_RESPONSE_STATUS_CODE_NUMBER, response_status, response_data) #add by wangjun 20131119 #设置用户权限验证模式 elif MESSAGE_ID_SET_AUTH_TYPE == in_message_id: rc_status = self.set_client_authorization_type(in_message_data) response_status = MESSAGE_ID_SUC response_data = MyChars.get_string_value( MyChars.STRING_HTTPSERVER_SET_AUTH_TYPE_SUC) log.debug_info(response_data) if not rc_status: response_status = MESSAGE_ID_ERROR response_data = MyChars.get_string_value( MyChars.STRING_HTTPSERVER_SET_AUTH_TYPE_FAIL) log.debug_info(response_data) self.response_run_methond_data(MESSAGE_ID_SET_AUTH_TYPE, response_status, response_data) #设置上传类型模式 elif MESSAGE_ID_SET_UPLOAD_TYPE == in_message_id: rc_status = self.set_client_upload_type(in_message_data) response_status = MESSAGE_ID_SUC response_data = MyChars.get_string_value( MyChars.STRING_HTTPSERVER_SET_UPLOAD_TYPE_SUC) log.debug_info(response_data) if not rc_status: response_status = MESSAGE_ID_ERROR response_data = MyChars.get_string_value( MyChars.STRING_HTTPSERVER_SET_UPLOAD_TYPE_FAIL) log.debug_info(response_data) self.response_run_methond_data(MESSAGE_ID_SET_UPLOAD_TYPE, response_status, response_data) else: rsp_string_data = MyChars.get_string_value( MyChars. STRING_HTTPSERVER_DISPATH_REQUEST_MESSGAE_NOT_FOUND_MATCH_INTERFACE ) log.debug_info(rsp_string_data) self.response_run_methond_data(in_message_id, MESSAGE_ID_ERROR, rsp_string_data) except Exception, e: #dispatch_request_message_data method #log.debug_info(e) rsp_string_data = MyChars.get_string_value( MyChars.STRING_HTTPSERVER_DISPATH_REQUEST_MESSGAE_DATA_FAIL) log.debug_info(rsp_string_data) self.response_run_methond_data(in_message_id, MESSAGE_ID_ERROR, rsp_string_data)
class MyHttpServer(BasePIPEChildControl): 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) def __del__(self): #销毁基类 BasePIPEChildControl.__del__(self) def start_httpservet(self): """ 启动Twisted Http Server """ if not self.http_factory: rsp_string_data = MyChars.get_string_value( MyChars.STRING_HTTPSERVER_FACTORY_OBJECT_HANDLE_NOT_INIT) log.debug_info(rsp_string_data) raise RuntimeError(rsp_string_data) #将异常抛到上层接口 log.debug_info(u"start_httpservet start") try: log.debug_info(u"reactor.listenTCP start") #设置监听对象 reactor.listenTCP(self.http_port, self.http_factory, interface=self.http_address) except Exception, e: #reactor.listenTCP mthod #add by wangjun 20131127 #停止SERVER监听网络 reactor.stop if isinstance(e, CannotListenError): rsp_string_data = MyChars.get_string_value( MyChars.STRING_HTTPSERVER_REACTOR_CANNOT_LISTEN_THIS_PORT ) % (self.http_address, self.http_port) else: rsp_string_data = MyChars.get_string_value( MyChars.STRING_HTTPSERVER_REACTOR_LISTEN_TCP_ERROR) log.debug_info("-------------------------------------") log.debug_info(rsp_string_data) log.debug_info("-------------------------------------") raise RuntimeError(rsp_string_data) #将异常抛到上层接口 try: log.debug_info(u"reactor.run start") #启动SERVER监听网络 reactor.run() except Exception, e: #reactor.run method #log.debug_info(e) rsp_string_data = MyChars.get_string_value( MyChars.STRING_HTTPSERVER_REACTOR_RUN_ERROR) log.debug_info(rsp_string_data) raise RuntimeError(rsp_string_data) #将异常抛到上层接口
preline = request.content.readline() remainbytes -= len(preline) while remainbytes > 0: line = request.content.readline() remainbytes -= len(line) if boundary in line: preline = preline[0:-1] if preline.endswith('\r'): preline = preline[0:-1] out.write(preline) out.close() rsp_string_data = MyChars.get_string_value( MyChars.STRING_REQUEST_RENDER_UPLOAD_FILE_SUCCESS) return (True, rsp_string_data % fn) else: out.write(preline) preline = line rsp_string_data = MyChars.get_string_value( MyChars.STRING_REQUEST_RENDER_ERROR_UNEXPECT_ENDS_OF_DATA) return (False, rsp_string_data) def deal_put_data(self, request): """ PUT方法数据处理方法 """
def deal_get_data(self, request): """ GET方法数据处理方法 """ #获取文件地址信息 self.path = request.path path = self.translate_path(self.path, request) f = None #转化编码格式 path = MyChars.convert_coding(path) if os.path.isdir(path): if not self.path.endswith('/'): #重定向浏览器 request.setResponseCode(301) request.setHeader("Location", self.path + "/") rsp_string_data = MyChars.get_string_value( MyChars. STRING_REQUEST_RENDER_ERROR_NO_PERMISSION_TO_LIST_DIRECTORY ) request.write( MyChars.build_value_type_unicode_to_string( rsp_string_data)) request.finish() return None #文件地址下是否存在index HTML文件 for index in "index.html", "index.htm": index = os.path.join(path, index) if os.path.exists(index): path = index break else: #创建一个以目录列表文件为数据来源的HTML文件,并将数据流句柄做为返回值 return self.list_directory(path, request) #获取文件类型,获取到的数据用于MIME Content-type头 ctype = self.guess_type(path) #初始化f文件句柄 try: f = open(path, 'rb') except IOError: request.setResponseCode(http.NOT_FOUND) rsp_string_data = MyChars.get_string_value( MyChars.STRING_REQUEST_RENDER_404_ERROR) request.write( MyChars.build_value_type_unicode_to_string(rsp_string_data)) request.finish() return None #获取文件的大小,以位为单位 fs = os.fstat(f.fileno()) request.setResponseCode(200) request.setHeader("Content-type", "%s; charset=utf-8" % ctype) request.setHeader("Content-Length", str(fs[6])) last_modified_data = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(fs.st_mtime)) request.setHeader("Last-Modified", last_modified_data) return f
def process_authorization_digest(self, authorization_data): """ 调用digest权限验证 """ #保存验证状态,-1表示账户未注册,-2表示密码认证失败 check_request_digest_authorization_stauts = 0 #解析客户端digest认证数据 request_auth_digest_data_dict = DigestAuthorization.parse_request_digest_authorization_data( authorization_data, self.method) #print request_auth_digest_data_dict #查找用户名是否注册,并返回注册的密码数据 client_request_username = request_auth_digest_data_dict.get('username') rc_status, rc_client_register_password = self._get_digest_authorization_uername_register_password( client_request_username) if not rc_status: #账户未注册 check_request_digest_authorization_stauts = -1 else: #配置约定数据 cfg_auth_digest_option = {} cfg_auth_digest_option['username'] = client_request_username cfg_auth_digest_option['password'] = rc_client_register_password cfg_auth_digest_option['realm'] = CFG_AUTH_RELM_VALUE #进行客户端digest权限验证 check_suc_flag = DigestAuthorization.check_digest_authorization_data( cfg_auth_digest_option, request_auth_digest_data_dict) if check_suc_flag: #权限认证通过 check_request_digest_authorization_stauts = 1 #处理客户端具体数据请求 self.process_dispath_request() else: #密码认证失败 check_request_digest_authorization_stauts = -2 #授权不成功 if 1 != check_request_digest_authorization_stauts: if -2 == check_request_digest_authorization_stauts: #授权不成功 rsp_string_data = MyChars.get_string_value( MyChars.STRING_REQUEST_PROCESS_401_ERROR) else: #账户未注册 rsp_string_data = MyChars.get_string_value( MyChars.STRING_REQUEST_CRED_NO_SUCH_USER) #获取返回数据长度 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)) #未授权的, 构建Digest认证返回信息 self.setHeader("WWW-Authenticate", self._construct_auth_digest_response()) self.write( MyChars.build_value_type_unicode_to_string(rsp_string_data)) self.finish()
def deal_post_data(self, request): """ POST方法数据处理方法 ++++++++++++++++++++++++++++++++++++++++++++ 数据内容格式: "" -----------------------------7dd17727d02ae-- Content-Disposition: form-data; name="file"; filename="C:\test.txt"\r\n Content-Type: text/plain\r\n \r\n 要上传的数据流 -----------------------------7dd17727d02ae-- "" ++++++++++++++++++++++++++++++++++++++++++++ """ #获取content-type头 contenttype = request.getHeader('content-type') boundary = None if contenttype: boundary = contenttype.split("=")[1] #获取content-length头 remainbytes = int(request.getHeader('content-length')) #数据以boundary开头 line = request.content.readline() remainbytes -= len(line) if not boundary or not boundary in line: rsp_string_data = MyChars.get_string_value( MyChars. STRING_REQUEST_RENDER_ERROR_POST_CONTENT_NOT_BEGIN_WITH_BOUNDAR ) return (False, rsp_string_data) #获取Content-Disposition数据内容 line = request.content.readline() remainbytes -= len(line) fn = re.findall(r'Content-Disposition.*name="file"; filename="(.*)"', line) if not fn: rsp_string_data = MyChars.get_string_value( MyChars.STRING_REQUEST_RENDER_ERROR_CAN_NOT_FINE_OUT_FILENAME) return (False, rsp_string_data) #获取文件名字和存放路径 self.path = request.path path = self.translate_path(self.path, request) osType = platform.system() filedir, cfilename = os.path.split(fn[0]) filename = cfilename try: if osType == "Linux": fn = os.path.join(path, fn[0].decode('gbk').encode('utf-8')) else: fn = os.path.join(path, filename) except Exception, e: rsp_string_data = MyChars.get_string_value( MyChars.STRING_REQUEST_RENDER_ERROR_FILENAME_ERROR) return (False, rsp_string_data)
class MyRenderPage(resource.Resource): if not mimetypes.inited: mimetypes.init() #try to read system mime.types extensions_map = mimetypes.types_map.copy() extensions_map.update({ '': 'application/octet-stream', # Default '.py': 'text/plain', '.c': 'text/plain', '.h': 'text/plain', }) def __init__(self, in_home_workspace_dir): resource.Resource.__init__(self) #将路径转为系统默认编码 #modify by wangjun 20131224 if isinstance(in_home_workspace_dir, unicode): in_home_workspace_dir = in_home_workspace_dir.encode("utf8") self.home_workspace_dir = in_home_workspace_dir def __del__(self): pass def render_GET(self, request): """ GET响应接口 """ f = self.deal_get_data(request) if f: self.wirte_result_data(f, request) f.close() def render_POST(self, request): """ POST响应接口 """ r, info = self.deal_post_data(request) f = StringIO() f.write('<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">') f.write( '<html>\n<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head>\n<title>Upload Result Page</title>\n' ) f.write('<body>\n<h2>Upload Result Page</h2>\n') f.write('<hr>\n') if r: f.write("<strong>Success:</strong>") else: f.write("<strong>Failed:</strong>") f.write(MyChars.build_value_type_unicode_to_string(info)) f.write("<br><a href=\"%s\">back</a>" % request.getHeader('referer')) f.write("<hr><small>Powered By: bones7456, check new version at ") f.write("<a href=\"http://li2z.cn/?s=SimpleHTTPServerWithUpload\">") f.write("here</a>.</small></body>\n</html>\n") length = f.tell() f.seek(0) request.setResponseCode(200) request.setHeader("Content-type", "text/html; charset=utf-8") request.setHeader("Content-Length", str(length)) if f: self.wirte_result_data(f, request) f.close() def render_PUT(self, request): """ PUT响应接口 """ r, info = self.deal_put_data(request) f = StringIO() f.seek(0) f.write(MyChars.build_value_type_unicode_to_string(info)) request.setResponseCode(200) request.setHeader("Content-Length", 0) if f: self.wirte_result_data(f, request) f.close() def deal_get_data(self, request): """ GET方法数据处理方法 """ #获取文件地址信息 self.path = request.path path = self.translate_path(self.path, request) f = None #转化编码格式 path = MyChars.convert_coding(path) if os.path.isdir(path): if not self.path.endswith('/'): #重定向浏览器 request.setResponseCode(301) request.setHeader("Location", self.path + "/") rsp_string_data = MyChars.get_string_value( MyChars. STRING_REQUEST_RENDER_ERROR_NO_PERMISSION_TO_LIST_DIRECTORY ) request.write( MyChars.build_value_type_unicode_to_string( rsp_string_data)) request.finish() return None #文件地址下是否存在index HTML文件 for index in "index.html", "index.htm": index = os.path.join(path, index) if os.path.exists(index): path = index break else: #创建一个以目录列表文件为数据来源的HTML文件,并将数据流句柄做为返回值 return self.list_directory(path, request) #获取文件类型,获取到的数据用于MIME Content-type头 ctype = self.guess_type(path) #初始化f文件句柄 try: f = open(path, 'rb') except IOError: request.setResponseCode(http.NOT_FOUND) rsp_string_data = MyChars.get_string_value( MyChars.STRING_REQUEST_RENDER_404_ERROR) request.write( MyChars.build_value_type_unicode_to_string(rsp_string_data)) request.finish() return None #获取文件的大小,以位为单位 fs = os.fstat(f.fileno()) request.setResponseCode(200) request.setHeader("Content-type", "%s; charset=utf-8" % ctype) request.setHeader("Content-Length", str(fs[6])) last_modified_data = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(fs.st_mtime)) request.setHeader("Last-Modified", last_modified_data) return f def deal_post_data(self, request): """ POST方法数据处理方法 ++++++++++++++++++++++++++++++++++++++++++++ 数据内容格式: "" -----------------------------7dd17727d02ae-- Content-Disposition: form-data; name="file"; filename="C:\test.txt"\r\n Content-Type: text/plain\r\n \r\n 要上传的数据流 -----------------------------7dd17727d02ae-- "" ++++++++++++++++++++++++++++++++++++++++++++ """ #获取content-type头 contenttype = request.getHeader('content-type') boundary = None if contenttype: boundary = contenttype.split("=")[1] #获取content-length头 remainbytes = int(request.getHeader('content-length')) #数据以boundary开头 line = request.content.readline() remainbytes -= len(line) if not boundary or not boundary in line: rsp_string_data = MyChars.get_string_value( MyChars. STRING_REQUEST_RENDER_ERROR_POST_CONTENT_NOT_BEGIN_WITH_BOUNDAR ) return (False, rsp_string_data) #获取Content-Disposition数据内容 line = request.content.readline() remainbytes -= len(line) fn = re.findall(r'Content-Disposition.*name="file"; filename="(.*)"', line) if not fn: rsp_string_data = MyChars.get_string_value( MyChars.STRING_REQUEST_RENDER_ERROR_CAN_NOT_FINE_OUT_FILENAME) return (False, rsp_string_data) #获取文件名字和存放路径 self.path = request.path path = self.translate_path(self.path, request) osType = platform.system() filedir, cfilename = os.path.split(fn[0]) filename = cfilename try: if osType == "Linux": fn = os.path.join(path, fn[0].decode('gbk').encode('utf-8')) else: fn = os.path.join(path, filename) except Exception, e: rsp_string_data = MyChars.get_string_value( MyChars.STRING_REQUEST_RENDER_ERROR_FILENAME_ERROR) return (False, rsp_string_data) #如果文件已经存在则文件名加'_' while os.path.exists(fn): fn += "_" #获取Content-Type数据内容 line = request.content.readline() remainbytes -= len(line) line = request.content.readline() remainbytes -= len(line) try: #转换文件名编码为utf-8 fn = MyChars.convert_coding(fn) #add by wangjun 20131224 #打开文件 out = open(fn, 'wb') except Exception, e: rsp_string_data = MyChars.get_string_value( MyChars.STRING_REQUEST_RENDER_ERROR_CAN_NOT_WRITE_FILE) return (False, rsp_string_data)
def list_directory(self, path, request): """ 创建一个以目录列表文件为数据来源的HTML文件,并将数据流句柄做为返回值 """ try: list_dir_data = os.listdir(path) except os.error: request.setResponseCode(http.NOT_FOUND) rsp_string_data = MyChars.get_string_value( MyChars. STRING_REQUEST_RENDER_ERROR_NO_PERMISSION_TO_LIST_DIRECTORY) request.write( MyChars.build_value_type_unicode_to_string(rsp_string_data)) request.finish() return None #排序文件列表 list_dir_data.sort(key=lambda a: a.lower()) #构建HTML文件 f = StringIO() displaypath = cgi.escape(urllib.unquote(self.path)) #add by wangjun 20131224 displaypath_string = MyChars.convert_coding(displaypath) if isinstance(displaypath_string, unicode): displaypath_string = displaypath_string.encode("utf8") #写HTML TITLE数据 f.write('<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">') f.write( '<html>\n<head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head>\n<title>Directory listing for %s</title>\n' % displaypath_string) f.write('<body>\n<h2>Directory listing for %s</h2>\n' % displaypath_string) f.write('<hr>\n') """ #写HTML表单数据(upload) f.write("<form ENCTYPE=\"multipart/form-data\" method=\"post\">") f.write("<input name=\"file\" type=\"file\"/>") f.write("<input type=\"submit\" value=\"upload\"/>") f.write("              ") f.write("<input type=\"button\" value=\"HomePage\" onClick=\"location='/'\">") f.write("</form>\n") f.write("<hr>\n<ul>\n") """ #写文件列表数据 #这里数据节点的数据全部为str类型 for name in list_dir_data: try: fullname = os.path.join(path, name) #获取文件是否文件夹属性 temp_isdir_flag = os.path.isdir(fullname) #获取文件是否连接的属性 temp_islink_flag = os.path.islink(fullname) #转换编码格式 if isinstance(name, unicode): name = name.encode("utf8") #[step-1] temp_workspace_dir = MyChars.convert_coding( self.home_workspace_dir) if isinstance(temp_workspace_dir, unicode): temp_workspace_dir = temp_workspace_dir.encode("utf8") filename = temp_workspace_dir + '/' + displaypath + name #转化编码格式 filename = MyChars.convert_coding(filename) #获取文件长度 file_size = sizeof_fmt(os.path.getsize(filename)) #获取文件修改日期 modif_date = modification_date(filename) #[step-2] colorName = name linkname = name #追加文件和连接 if temp_isdir_flag: colorName = '<span style="background-color: #CEFFCE;">' + name + '/</span>' linkname = name + "/" if temp_islink_flag: colorName = '<span style="background-color: #FFBFFF;">' + name + '@</span>' #文件显示名称 if isinstance(colorName, unicode): colorName = colorName.encode("utf8") #文件或文件夹连接地址 url_item = urllib.quote(copy.deepcopy(linkname)) #将数据写入到HTML数据体中 f.write( '<table><tr><td width="60%%"><a href="%s">%s</a></td><td width="20%%">%s</td><td width="20%%">%s</td></tr>\n' % (url_item, colorName, file_size, modif_date)) except Exception, e: log.debug_info(u"MyRenderPage:list_directory coding error")