zoukankan      html  css  js  c++  java
  • python:configparser模块

    #!usr/bin/env python
    # -*- coding:utf-8 -*-

    __author__ = "Samson"
    #该模块用于生成和修改常见配置文档,当前模块的名称在 python 3.x 版本中变更为 configparser;python2中为ConfigParser
    #来看一个好多软件常见文档格式如下:
    # [DEFAULT]
    # ServerAliveInterval = 45
    # Compression = yes
    # CompressionLevel = 9
    # ForwardX11 = yes
    #
    # [bitbucket.org]
    # User = hg
    #
    # [topsecret.server.com]
    # Port = 50022
    # ForwardX11 = no

    #用python生成该种类型的文档
    import configparser
    config = configparser.ConfigParser()#用于生成一个configparser对象
    config["DEFAULT"] = {'ServerAliveInterval': '45',
    'Compression': 'yes',
    'CompressionLevel': '9'}

    config['bitbucket.org'] = {}
    config['bitbucket.org']['User'] = 'hg'
    config['topsecret.server.com'] = {}
    topsecret = config['topsecret.server.com']
    topsecret['Host Port'] = '50022' # mutates the parser
    topsecret['ForwardX11'] = 'no' # same here
    config['DEFAULT']['ForwardX11'] = 'yes'
    with open('example.ini', 'w') as configfile:
    config.write(configfile)


    #读取该模块
    conf = configparser.ConfigParser()
    conf.read("example.ini")
    print(conf.default_section)#打印默认default标签
    print(conf.sections())
    print(conf["topsecret.server.com"]["forwardx11"])
    #删除某个节点
    sec = conf.remove_section("topsecret.server.com")
    conf.write(open("example2.cfg","w"))
  • 相关阅读:
    各进制转换
    免root xshell连接termux
    sqlmap怎么拿shell
    SSRF漏洞
    国外安全网站、社区论坛、博客、公司、在线工具等整合收集
    渗透测试常用工具问题总结
    cdn绕过
    xss注入
    永恒之蓝(msf17010)kali复现
    文件上传漏洞和绕过
  • 原文地址:https://www.cnblogs.com/cansun/p/8179609.html
Copyright © 2011-2022 走看看