Уведомления

Группа в Telegram: @pythonsu

#1 Авг. 23, 2016 19:50:25

ivanzzzz
Зарегистрирован: 2016-08-23
Сообщения: 30
Репутация: +  0  -
Профиль   Отправить e-mail  

Подключение через прокси по imap и pop3

Переопределил методы для подключения к imap и pop3 через прокси:

 class SOCKS_IMAP4(imaplib.IMAP4):
    def __init__(self, host, port = imaplib.IMAP4_PORT,proxy_type = None, proxy = None):
            self.proxy = proxy
            self.proxy_type = proxy_type
            imaplib.IMAP4.__init__(self, host, port)
    def open(self,host,port=imaplib.IMAP4_PORT):
        self.host = host
        self.port = port
        self.sock = socksocket()        
        if self.proxy and self.proxy_type:
            proxy_ip,proxy_port = self.proxy.split(':')
            self.sock.setproxy(self.proxy_type,proxy_ip,int(proxy_port))
        self.sock.connect((host,port))
        self.file = self.sock.makefile('rb')
class SOCKS_IMAP4_SSL(imaplib.IMAP4_SSL):
    def __init__(self, host, port = imaplib.IMAP4_SSL_PORT,proxy_type = None, proxy = None,):
            self.proxy = proxy
            self.proxy_type = proxy_type
            imaplib.IMAP4_SSL.__init__(self, host, port)
    def open(self, host, port=imaplib.IMAP4_SSL_PORT):
        self.host = host
        self.port = port
        self.sock = socksocket()
        #actual privoxy default setting, but as said, you may want to parameterize it
        if self.proxy and self.proxy_type:
            proxy_ip,proxy_port = self.proxy.split(':')
            self.sock.setproxy(self.proxy_type,proxy_ip,int(proxy_port))
        self.sock.connect((host,port))
        self.sslobj = ssl.wrap_socket(self.sock, self.keyfile, self.certfile)
        self.file = self.sslobj.makefile('rb')

 class SOCKS_POP3(poplib.POP3):
    def __init__(self, host, port=poplib.POP3_PORT,timeout=socket._GLOBAL_DEFAULT_TIMEOUT,proxy_type = None, proxy = None):
        self.host = host
        self.port = port
        self.sock = socksocket()  
        if proxy and proxy_type:
            proxy_ip,proxy_port = proxy.split(':')
            self.sock.setproxy(proxy_type,proxy_ip,int(proxy_port))
        self.sock.connect((host, port))
        self.file = self.sock.makefile('rb')
        self._debugging = 0
        self.welcome = self._getresp()
class SOCKS_POP3_SSL(poplib.POP3_SSL):
        def __init__(self, host, port = poplib.POP3_SSL_PORT, keyfile = None, certfile = None,proxy_type = None, proxy = None):
            self.host = host
            self.port = port
            self.keyfile = keyfile
            self.certfile = certfile
            self.buffer = ""
            #msg = "getaddrinfo returns an empty list"
            self.sock = socksocket()  
            if proxy and proxy_type:
                proxy_ip,proxy_port = proxy.split(':')
                self.sock.setproxy(proxy_type,proxy_ip,int(proxy_port))
            self.sock.connect((host, port))
            #for res in socket.getaddrinfo(self.host, self.port, 0, socket.SOCK_STREAM):
            #    af, socktype, proto, canonname, sa = res
            #    try:
            #        self.sock = socket.socket(af, socktype, proto)
            #        self.sock.connect(sa)
            #    except socket.error, msg:
            #        if self.sock:
            #            self.sock.close()
            #        self.sock = None
            #        continue
            #    break
            #if not self.sock:
            #    raise socket.error, msg
            self.file = self.sock.makefile('rb')
            self.sslobj = ssl.wrap_socket(self.sock, self.keyfile, self.certfile)
            self._debugging = 0
            self.welcome = self._getresp()

но не всегда получается подключится, хотя через thunderbird подключается. В чем может быть проблема?

Офлайн

Board footer

Модераторировать

Powered by DjangoBB

Lo-Fi Version