美文网首页
如何通过http传输配置文件.ini中的内容

如何通过http传输配置文件.ini中的内容

作者: 简单可靠才可信 | 来源:发表于2019-08-21 18:38 被阅读0次

    target_file.ini文件下的内容

    #test file
    [DEFAULT]
    
    [database]
    ip = 192.168.1.254
    
    [_00_Token_Server]
    address = http://localhost
    port = 8000
    
    [_8001_Manager]
    address = http://localhost
    port = 8001
    standard_iron_count = 20
    standard_segment_count = 30
    standard_reset_count = 100
    _valid_skills = ['skill_1', 'skill_2', 'skill_3', 'skill_4']
    
    [_8002]
    address = http://localhost
    port = 8002
    
    [_8004]
    address = http://localhost
    port = 8004
    
    [_8005]
    address = http://localhost
    port = 8005
    
    [_8010]
    address = http://localhost
    port = 8010
    
    [_8011]
    address = http://localhost
    port = 8011
    
    # 中文
    [_8012]
    address = http://localhost
    port = 8012
    cooling_time = 10
    full_energy = 10
    
    [_8013]
    address = http://localhost
    port = 8013
    

    target_test.py文件下的内容

    import json
    import tormysql
    import time
    import os
    import datetime
    import configparser
    import random
    from aiohttp import web
    from aiohttp import ClientSession
    
    
    CONFIG = configparser.ConfigParser()
    CONFIG.read('./target_file.ini', encoding="utf-8")
    
    
    class StageSystemClass:
        def __init__(self, *args, **kwargs):
            pass
    
        async def demo(self):
            return CONFIG._sections
    
    
    MANAGER = StageSystemClass()
    ROUTES = web.RouteTableDef()
    
    
    def _json_response(body: dict = '', **kwargs) -> web.Response:
        '''
        A simple wrapper for aiohttp.web.Response return value.
        '''
        kwargs['body'] = json.dumps(body or kwargs['kwargs']).encode('utf-8')
        kwargs['content_type'] = 'text/json'
        return web.Response(**kwargs)
    
    
    @ROUTES.post('/demo')
    async def _demo(request: web.Request) -> web.Response:
        result = await MANAGER.demo()
        return _json_response(result)
    
    
    def run():
        app = web.Application()
        app.add_routes(ROUTES)
        web.run_app(app, port=8888)
    
    
    if __name__ == "__main__":
        run()
    

    request_test.py文件下的内容

    import json
    import requests
    # import target_test
    response = requests.post("http://localhost:8888/demo")
    dict_d = json.loads(response.text, encoding="utf-8")
    for key in dict_d.keys():
        print(dict_d[key])
    print(response.text)
    input("测试完成")
    

    测试结果如下图

    image.png

    从图中可以看出.ini配置文件可以通过http传输,传输的格式为json格式

    相关文章

      网友评论

          本文标题:如何通过http传输配置文件.ini中的内容

          本文链接:https://www.haomeiwen.com/subject/ogiisctx.html