Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
81 views
in Technique[技术] by (71.8m points)

python - passing a configparser.ConfigParser() object via __init__?

i'm currently working on a project for dns-enumeration, which sends requests to various APIs. Some of these APIs require an API-Key, which i provide in a config.ini file. In my current setup I use configparser to read-in the different values into an object, so i can access the object when needed. 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.

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:

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:

 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:

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.

question from:https://stackoverflow.com/questions/65843283/passing-a-configparser-configparser-object-via-init

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I figured it out myself after getting input from @Jakub Szlaur:

My file-path pointed to the wrong folders, therefore the config.ini file was never reached.

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! ;-)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...