requests库如何解析配置文件

use_config.ini为配置文件,protocal,host,requestpath

# coding:utf-8
import requests
import json
import pprint
import time
from tools.parse_confile import ParseConfile


class new_request(object):
    def __init__(self):
        self.cf = ParseConfile()
        self.protocal = self.cf.get_account('new_demo', 'protocal')
        self.host = self.cf.get_account('new_demo', 'host')
        self.requestPath = self.cf.get_account('new_demo', 'requestpath')
        self.url = self.protocal + self.host + self.requestPath

    def response_get(self, corpus):
        response = requests.request('get', url=self.url + corpus)
        # time.sleep(0.1)
        responseText = response.text
        return responseText

    def response_handle(self, reponseText):
        response_data = json.loads(reponseText)
        result_data = response_data['data']
        if not result_data:
            return "[]"
        else:
            return result_data[0]['intent_path']

    def get_child_intent(self, reponseText):
        response_data = json.loads(reponseText)
        result_data = response_data['data']
        if result_data == []:
            return "[]"
        elif (result_data != []) & (result_data[0]['child_intent'] == []):
            return "[]"
        else:
            return result_data[0]['child_intent'][0]['intent_path']

    def get_children_intent(self, reponseText):
        response_data = json.loads(reponseText)
        result_data = response_data['data']
        if result_data == []:
            return "[]"
        elif (result_data != []) & (result_data[0]['child_intent'] == []):
            return "[]"
        else:
            return result_data[0]['child_intent'][0]['intent_path']


if __name__ == '__main__':
    data = json.loads(new_request().response_get("请假"))
    pprint.pprint(data)

parse_confile.py

# coding:utf-8
import configparser
from requestconfig.use_config import *


class ParseConfile(object):
    def __init__(self):
        self.file = request_dir
        self.conf = configparser.ConfigParser()
        self.read = self.conf.read(self.file)

    # 获取所有的sections,返回一个列表
    def get_sections(self):
        return self.conf.sections()

    # 获取指定section下所有的option,返回列表
    def get_options(self, section):
        return self.conf.options(section)
        
	# 获取指定section,指定option对应的数据,返回元组和字符串
    def get_account(self, section, option):
        try:
            locator = self.conf.get(section, option)
            if '->' in locator:
                locator = tuple(locator.split('->'))
            return locator
        except configparser.NoOptionError as e:
            print('error:', e)
        return 'error: No option "{}" in section : "{}" '.format(option, section)
	
	# 获取指定section下所有的option和对应的数据,返回字典
    def get_value(self, section):
        value = dict(self.conf.items(section))
        return value


if __name__ == '__main__':
    print(ParseConfile().get_sections())
    print(ParseConfile().get_options('new_demo'))
    print(ParseConfile().get_account('new_demo', 'host'))
    print(ParseConfile().get_value('new_demo'))



评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值