def __init__(self, request, client, encoding='utf-8'): self.__client = client[0] self.__host = client[1][0] self.__port = client[1][1] if not isinstance(request, str): request = str(request, encoding=encoding) _slpit = request.split() _split_r_n = request.split('\r\n\r\n', 1) try: self.method = _slpit[0] self.path = _slpit[1] self.protocol_version = _slpit[2] self.headers = _split_r_n[0].split('\r\n')[1:] self.body = _split_r_n[1] except: self.method = False self.path = False self.protocol_version = False self.headers = False self.body = False syslog("Request解析错误") if self.headers != False: try: _headers = Header() for item in self.headers: slpit = item.split(': ') _headers.set(slpit[0], slpit[1]) self.headers = _headers except: syslog("Request Header解析错误") self.headers = False
def get(self, option): try: result = self.content[option] return result except: syslog("Request Header不存在键:", option) return False
def get_option_value(self, section, option): try: container = self.__config.get(section, option) except: syslog("获取配置文件", self.__file_name, "键值发生错误:", section, option) return False return container
def set_option_value(self, section, option, value): try: self.__config.set(section, option, value) file = open(self.__file_name) self.__config.write(file) file.close() return True except: syslog("设置配置文件", self.__file_name, "键值发生错误:", section, option) return False
def __init__(self, config_file_name, encoding='utf8'): self.__file_name = config_file_name self.__config = configparser.ConfigParser() self.__config.read(config_file_name, encoding=encoding) try: file = open(self.__file_name) file.close() except: syslog('打开配置文件时发生错误', config_file_name) if self.__config.sections() == []: syslog("配置文件内容丢失:", config_file_name)