编写配置文件时,使用ConfigParser而不是常规的python.py文件有什么好处? [英] What is the benefit of using the ConfigParser instead of a regular python.py file when writing configuration files?

查看:86
本文介绍了编写配置文件时,使用ConfigParser而不是常规的python.py文件有什么好处?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一段时间以来,我一直在使用ConfigParser模块写入配置文件.但是,最近有个念头打动了我.为什么不只使用纯Python?以以下示例配置文件为例:

I have been using the ConfigParser module to write configuration files for some time. However, a thought recently struck me; why not just use pure Python instead? Take this example configuration file:

[parameters]
# Host
host = stackoverflow.com
port = 22

要读取这些值到我的代码中,

To read these values into my code, I do

import ConfigParser
config = ConfigParser.SafeConfigParser()
config.read('host.cfg')

host = config.get('parameters', 'host')
port = config.get('parameters', 'port')

另一方面,如果我有这样的配置文件:

On the other hand, if I had a config file like this:

# Host
host = 'stackoverflow.com'
port = 22

在我的主要代码中,我可以这样做:

In my main code, I could do this:

from host_cfg import *

那么,使用ConfigParser模块有什么好处?每种方法的优缺点是什么?

So what do I gain from using the ConfigParser module? What are the pros and cons of each approach?

推荐答案

那我可以从使用ConfigParser模块中学到什么?

So what do I gain from using the ConfigParser module?

与Windows .ini文件的兼容性.使某些人高兴.

Compatibility with Windows .ini files. Makes some people happy.

每种方法的优缺点是什么?

What are the pros and cons of each approach?

ConfigParser的语法有限,并且某些相对简单的内容非常虚构.请查看logging作为示例.

ConfigParser has limited syntax and some relatively simple things get very contrived. Look at logging for examples.

Python语法更简单,更易于使用.没有安全漏洞,因为没有人会浪费时间去破解配置文件,因为他们只需要破解您的源代码即可.实际上,他们可以破解大多数内置的Python库.为此,他们可以自己煮Python解释器.

Python syntax is simpler and much easier to work with. There's no security hole, since no one will waste time hacking a config file when they can simply hack your source code. Indeed, they can hack most of the built-in Python library. For that matter, they could cook the Python interpreter itself.

当您的应用程序源代码中的其他地方存在许多容易得多的漏洞利用时,没有人会浪费时间来破解配置文件.

No one wastes time hacking config files when there are much, much easier exploits elsewhere in your application source code.

这篇关于编写配置文件时,使用ConfigParser而不是常规的python.py文件有什么好处?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆