0

I'm using ConfigParser to pull IMAP details for email login which works fine on Linux, however I keep getting socket.gaierror: [Errno 11001] getaddrinfo failed when running on Windows. I'm using Python 3.8.

This is the code causing the error.

self._config = configparser.ConfigParser()
self._config.read('config.ini')
self._mail = imaplib.IMAP4_SSL(self._config.get('imap', 'host'),993) # <- the error

I thought it may have been a race condition, the class initializing before ConfigParser was able to initialize properly however adding a print in there to read out the host worked fine. Pasting the actual imap url string in place of the self._config.get works

TehPirate
  • 154
  • 1
  • 12
  • The ```getaddrinfo``` error relates to your host name. Is it an IP or an actual host name? The system can't seem to find that address.. take a look at https://stackoverflow.com/questions/7334199/getaddrinfo-failed-what-does-that-mean – ewokx Sep 10 '21 at 06:35
  • @ewong It is a hostname. When I put the string in manually it works fine, but not with ConfigParser – TehPirate Sep 10 '21 at 06:37
  • what does ```self._config.get('imap', 'host')``` return? What does your config look like? I'm somewhat guessing it's falling back on ```host``` as it can't find ```imap``` mentioned. – ewokx Sep 10 '21 at 07:46

1 Answers1

0

Im nut sure but i think you should setup ajson configfile then save the hoststring into a variable that you then use to connect. The final code then would look something like this:

import json
with open("config.json", "r+") as conf: # open in readmode and create the file if not exists
  config = json.load(conf)
  host = config.get("host")

The json file then would look like this:

{
  "host": "IP_Adress"
}

Edit: To check if it worked you can print out the host variable. this should print the value of "host" within the config.json

print(host)
  • I'm currently using ConfigParser, wasn't planning to use JSON. If this is the only way to make it work, I'll circle back to it – TehPirate Sep 10 '21 at 07:15