[英]passing a configparser.ConfigParser() object via __init__?
i'm currently working on a project for dns-enumeration, which sends requests to various APIs.我目前正在开发一个 dns-enumeration 项目,该项目向各种 API 发送请求。 Some of these APIs require an API-Key, which i provide in a config.ini file.
其中一些 API 需要一个 API 密钥,我在 config.ini 文件中提供了它。 In my current setup I use configparser to read-in the different values into an object, so i can access the object when needed.
在我当前的设置中,我使用 configparser 将不同的值读入 object,因此我可以在需要时访问 object。 Now, as I try to implement something like a class structure, i would like to read-in the config file once in the init of a parent class, so i can inherit every tool that needs an API-Key from that class.
现在,当我尝试实现类似 class 结构的东西时,我想在父 class 的init中读入一次配置文件,这样我就可以从 ZA2F2ED4F8EBC2CBB4C21A29DC4 继承每个需要 API 密钥的工具。
Right now the setup looks something like this:现在设置看起来像这样:
class Source:
def __init__(self):
config = configparser.ConfigParser()
config.read('./config.ini')
self.config = config
class BinaryEdge(Source):
def __init__(self):
super().__init__()
def query(self, domain, dnsprobe):
api_key = self.config['BINARYEDGE']['API-KEY']
url = 'https://api.binaryedge.io/v2/query/domains/subdomain/' + domain
fqdns = []
...
In my understanding, if i initiate a new BinaryEdge-Instance, for example like this:据我了解,如果我启动一个新的 BinaryEdge-Instance,例如这样:
if __name__ == "__main__":
BinaryEdge = BinaryEdge()
print(BinaryEdge.query("heise.de", False))
It technically should read in the config file into an object and pass it to the newly created object, so i can access it via self.config, something like this:从技术上讲,它应该将配置文件读入 object 并将其传递给新创建的 object,因此我可以通过 self.config 访问它,如下所示:
def query(self, domain, dnsprobe):
api_key = self.config['BINARYEDGE']['API-KEY']
url = 'https://api.binaryedge.io/v2/query/domains/subdomain/' + domain
fqdns = []
...
But when im debugging this setup, the config object stays default (and threrefore empty), which obviously leads straight into a key error:但是当我调试这个设置时,配置 object 保持默认(并且因此为空),这显然会直接导致一个关键错误:
File "/usr/lib64/python3.9/configparser.py", line 960, in __getitem__
raise KeyError(key)
KeyError: 'BINARYEDGE'
As im not as good in python programming as i would like to be, i'm struggling solving this error on my own and would be thankful for any advancing input.由于我在 python 编程方面不如我想的那样好,我正在努力自己解决这个错误,并感谢任何先进的输入。
I figured it out myself after getting input from @Jakub Szlaur:在收到@Jakub Szlaur 的意见后,我自己想通了:
My file-path pointed to the wrong folders, therefore the config.ini file was never reached.我的文件路径指向了错误的文件夹,因此从未到达 config.ini 文件。
After changing:更改后:
config.read('./config.ini')
to至
config.read('$HOME/$PROJECT_PATH/config.ini')
it worked as expected.它按预期工作。
I also changed the "Source"-Class according to the comments for "better code-style":我还根据“更好的代码样式”的评论更改了“源”类:
class Source:
def __init__(self):
self.config = self.readconfig('../config.ini')
def readconfig(self, filename):
config = configparser.ConfigParser()
config.read(filename)
return config
Thanks for the help;谢谢您的帮助; ;-)
;-)
The code looks like it should work (I can't find any errors).代码看起来应该可以工作(我找不到任何错误)。 Try checking your config file to see whether there really is such a key.
尝试检查您的配置文件,看看是否真的有这样的关键。
But about the code itself, there are a couple of things that I would recommend changing.但是关于代码本身,我建议更改几件事。
First, although reading the config is part of the initialisation of Source
, it would be better if you made it a function, then called that function:首先,虽然读取配置是
Source
初始化的一部分,但最好将其设为 function,然后调用 function:
class Source:
def __init__(self):
self.config = self.readconfig("./config.ini")
def readconfig(self, filename):
config = configparser.ConfigParser()
config.read(filename)
return config
Never do this: BinaryEdge = BinaryEdge()
.永远不要这样做:
BinaryEdge = BinaryEdge()
。 If you wanted to make another instance of BinaryEdge
, it would call the BinaryEdge variable instead.如果您想创建
BinaryEdge
的另一个实例,它将改为调用 BinaryEdge 变量。 Name it something different.给它取个不同的名字。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.