ConnectionResetError: [WinError 10054] An existing connection was forcibly closed by the remote host (while accessing Gmail emails)

你。 提交于 2021-02-19 07:54:26

问题


i'm getting the title error when I'm trying to access my gmail emails using the imap-tools module of python. Below is the details as given during establishing the connection.

    Traceback (most recent call last):
  File "d:\PythonLabs\delete_gmail_emails.py", line 3, in <module>
    gautham_gmail_box = MailBox('imap.gmail.com')
  File "C:\Users\skolluru\AppData\Local\Programs\Python\Python37-32\lib\site-packages\imap_tools\main.py", line 61, in __init__
    host, port or imaplib.IMAP4_SSL_PORT, keyfile, certfile, ssl_context)
  File "C:\Users\skolluru\AppData\Local\Programs\Python\Python37-32\lib\imaplib.py", line 1283, in __init__
    IMAP4.__init__(self, host, port)
  File "C:\Users\skolluru\AppData\Local\Programs\Python\Python37-32\lib\imaplib.py", line 197, in __init__
    self.open(host, port)
  File "C:\Users\skolluru\AppData\Local\Programs\Python\Python37-32\lib\imaplib.py", line 1296, in open
    IMAP4.open(self, host, port)
  File "C:\Users\skolluru\AppData\Local\Programs\Python\Python37-32\lib\imaplib.py", line 294, in open
    self.sock = self._create_socket()
  File "C:\Users\skolluru\AppData\Local\Programs\Python\Python37-32\lib\imaplib.py", line 1288, in _create_socket
    server_hostname=self.host)
  File "C:\Users\skolluru\AppData\Local\Programs\Python\Python37-32\lib\ssl.py", line 412, in wrap_socket
    session=session
  File "C:\Users\skolluru\AppData\Local\Programs\Python\Python37-32\lib\ssl.py", line 850, in _create
    self.do_handshake()
  File "C:\Users\skolluru\AppData\Local\Programs\Python\Python37-32\lib\ssl.py", line 1108, in do_handshake
    self._sslobj.do_handshake()
ConnectionResetError: [WinError 10054] An existing connection was forcibly closed by the remote host

[Done] exited with code=1 in 0.338 seconds

I think the error is because of my gmail email secured with 2-step authentication. I want to keep my security for my gmail account same want to know a workaround to access my emails from my python program.

Here is my python code:

    from imap_tools import MailBox

gautham_gmail_box = MailBox('imap.gmail.com')
gautham_gmail_box.login('my_email@gmail.com','my_password')

回答1:


I think you have to enable some unsecure access to your Gmail before you can access it this way, but I'm not sure.

IMO, you're much better off using Google's official library to access Gmail using Python. Visit https://developers.google.com/gmail/api/quickstart/python to learn more about this.



来源:https://stackoverflow.com/questions/52759754/connectionreseterror-winerror-10054-an-existing-connection-was-forcibly-close

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!