Home > Net >  Why does Python imaplib return Errno 111: Connection refused when I try to instantiate the IMAP4() o
Why does Python imaplib return Errno 111: Connection refused when I try to instantiate the IMAP4() o

Time:10-17

I am trying to use the Python imaplib library in GitPod.

Following the documentation, I imported imaplib and instantiated the IMAP4 object with the following line of code:

M = imaplib.IMAP4()

This returns the following message:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/gitpod/.pyenv/versions/3.8.12/lib/python3.8/imaplib.py", line 198, in __init__
    self.open(host, port)
  File "/home/gitpod/.pyenv/versions/3.8.12/lib/python3.8/imaplib.py", line 303, in open
    self.sock = self._create_socket()
  File "/home/gitpod/.pyenv/versions/3.8.12/lib/python3.8/imaplib.py", line 293, in _create_socket
    return socket.create_connection((host, self.port))
  File "/home/gitpod/.pyenv/versions/3.8.12/lib/python3.8/socket.py", line 808, in create_connection
    raise err
  File "/home/gitpod/.pyenv/versions/3.8.12/lib/python3.8/socket.py", line 796, in create_connection
    sock.connect(sa)
ConnectionRefusedError: [Errno 111] Connection refused

Is the problem that the imaplib is not able to connect to its own socket, and so I should somehow activate that socket inside GitPod first?

CodePudding user response:

From the documentation:

class imaplib.IMAP4(host='', port=IMAP4_PORT, timeout=None)
This class implements the actual IMAP4 protocol. The connection is created and protocol version (IMAP4 or IMAP4rev1) is determined when the instance is initialized. If host is not specified, '' (the local host) is used.

Thus, it tries to connect to the IMAP server on the local machine. Looks like there is none or at least not at port 143.

  • Related