Python IMAP代理连接

时间:2018-07-26 15:58:01

标签: python python-3.x imap socks imaplib

我尝试使用python 3.5通过SOCKS5代理登录到IMAP5服务器,但是它没有登录并显示此错误: 命令:LOGIN =>自动注销内部错误,我们会以ffcd2fca-96a9-4c64-89d5-361123783232的身份记住您

如果我不使用代理,那一切都很好。

我有一些问题

1。是因为代理服务器并且禁止IMAP4连接吗?

2。我该如何解决?

import ssl, time

from socks import create_connection
from socks import PROXY_TYPE_SOCKS4
from socks import PROXY_TYPE_SOCKS5
from socks import PROXY_TYPE_HTTP

from imaplib import IMAP4
from imaplib import IMAP4_PORT
from imaplib import IMAP4_SSL_PORT
from filter import get_user_pass

__author__ = "sstevan"
__license__ = "GPLv3"
__version__ = "0.1"


class SocksIMAP4(IMAP4):
    """
    IMAP service trough SOCKS proxy. PySocks module required.
    """

    PROXY_TYPES = {"socks4": PROXY_TYPE_SOCKS4,
                   "socks5": PROXY_TYPE_SOCKS5,
                   "http": PROXY_TYPE_HTTP}

    def __init__(self, host, port=IMAP4_PORT, proxy_addr=None, proxy_port=None,
                 rdns=True, username=None, password=None, proxy_type="socks5"):

        self.proxy_addr = proxy_addr
        self.proxy_port = proxy_port
        self.rdns = rdns
        self.username = username
        self.password = password
        self.proxy_type = SocksIMAP4.PROXY_TYPES[proxy_type.lower()]

        IMAP4.__init__(self, host, port)

    def _create_socket(self):
        return create_connection((self.host, self.port), proxy_type=self.proxy_type, proxy_addr=self.proxy_addr,
                                 proxy_port=self.proxy_port, proxy_rdns=self.rdns, proxy_username=self.username,
                                 proxy_password=self.password)


class SocksIMAP4SSL(SocksIMAP4):

    def __init__(self, host='', port=IMAP4_SSL_PORT, keyfile=None, certfile=None, ssl_context=None, proxy_addr=None,
                 proxy_port=None, rdns=True, username=None, password=None, proxy_type="socks5"):

        if ssl_context is not None and keyfile is not None:
                raise ValueError("ssl_context and keyfile arguments are mutually "
                                 "exclusive")
        if ssl_context is not None and certfile is not None:
            raise ValueError("ssl_context and certfile arguments are mutually "
                             "exclusive")

        self.keyfile = keyfile
        self.certfile = certfile
        if ssl_context is None:
            ssl_context = ssl._create_stdlib_context(certfile=certfile,
                                                     keyfile=keyfile)
        self.ssl_context = ssl_context

        SocksIMAP4.__init__(self, host, port, proxy_addr=proxy_addr, proxy_port=proxy_port,
                            rdns=rdns, username=username, password=password, proxy_type=proxy_type)

    def _create_socket(self):
        sock = SocksIMAP4._create_socket(self)
        server_hostname = self.host if ssl.HAS_SNI else None
        return self.ssl_context.wrap_socket(sock, server_hostname=server_hostname)

    def open(self, host='', port=IMAP4_PORT):
        SocksIMAP4.open(self, host, port)

def connect_proxy(imap_server, imap_port, proxy_addr, proxy_port, proxy_type, email, password):
    mailbox = SocksIMAP4SSL(host=imap_server, port=imap_port,
                            proxy_addr=proxy_addr, proxy_port=proxy_port, proxy_type=proxy_type)
    try:
        mailbox.login(email, password)
        print("We are here")
        print("OK ",)
    except Exception as e:
        print(e)
        return False
    print(mailbox.state)
    mailbox.logout()
    return True


if __name__ == "__main__":
    imap_server = "imap.rambler.ru"
    imap_port = 993

    proxy_addr = "188.120.224.172"
    proxy_port = 59923
    proxy_type = "socks5"
    email, password = get_user_pass("pm@mail11.rambler.ru:11")
    if email is not None:
        resp = connect_proxy(imap_server, imap_port, proxy_addr, proxy_port, proxy_type, email, password)
        #resp = connect(email, password, "smtp.rambler.ru")
    time.sleep(1)

EMAIL:PASSWORD对用于测试。不要偷它:)

1 个答案:

答案 0 :(得分:0)

据我了解,这可能是Rambler的错误,还是防止某些恶意用户(例如我)免受暴力破解帐户的微小功能。

如果有人能看到此主题并有一些解决方案,想法或此类问题,请随时与我联系。