如何通过python中的Institute代理通过IMAP获取电子邮件?

时间:2016-07-07 22:32:59

标签: python sockets proxy gmail-imap

我已经浏览了这些solutions for connecting IMAP under proxy

要添加的信息: 我正在尝试编写一个python代码,可以使用我的学术研究所的http,https和socks代理服务器下的imapclient从gmails IMAP服务器获取邮件  在没有任何代理处理的情况下尝试时,它曾用于提供错误

socket.error [101]网络无法访问

import imapclient
import pyzmail

imapObj = imapclient.IMAPClient('imap.gmail.com',ssl=True)
imapObj.login('***********@gmail.com','*********')
imapObj.select_folder('INBOX', readonly=True)

UIDs = imapObj.search(['SINCE 07-Jul-2016'])

for item in UIDs:
    rawMessages = imapObj.fetch(item, ['BODY[]', 'FLAGS'])
    message = pyzmail.PyzMessage.factory(rawMessages[item]['BODY[]'])
    message.get_subject()
    message.get_addresses('from')
    message.get_addresses('to')
    message.get_addresses('cc')
    message.get_addresses('bcc')
    message.text_part != None
    message.text_part.get_payload().decode(message.text_part.charset)
    message.html_part != None
    message.html_part.get_payload().decode(message.html_part.charset)
imapObj.logout()

但是,该过程会出现错误

File "mailtotext.py", line 16, in <module>
imapObj = imapclient.IMAPClient('imap.gmail.com',ssl=True)
  File "/usr/local/lib/python2.7/dist-packages/imapclient/imapclient.py", line 152, in __init__
    self._imap = self._create_IMAP4()
  File "/usr/local/lib/python2.7/dist-packages/imapclient/imapclient.py",line 164, in _create_IMAP4
self._timeout)
  File "/usr/local/lib/python2.7/dist-packages/imapclient/tls.py", line 153, in __init__
    imaplib.IMAP4.__init__(self, host, port)
  File "/usr/lib/python2.7/imaplib.py", line 172, in __init__
self.open(host, port)
  File "/usr/local/lib/python2.7/dist-packages/imapclient/tls.py", line 158, in open
    sock = socket.create_connection((host, port), self._timeout)
  File "/usr/lib/python2.7/socket.py", line 571, in create_connection
raise err
socket.error: [Errno 101] Network is unreachable

然后我按照上面提到的链接程序,因为我的学院代理为http,https,socks 我已将系统代理设置设置为

http_proxy="http://10.3.100.207:8080/"
https_proxy="https://10.3.100.207:8080/"
ftp_proxy="ftp://10.3.100.207:8080/"
socks_proxy="socks://10.3.100.207:8080/"

并将代码编辑为     导入imapclient     导入pyzmail     进口袜子     导入套接字

socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS4,"10.3.100.207",8080,True)
socket.socket = socks.socksocket

imapObj = imapclient.IMAPClient('imap.gmail.com',ssl=True)
imapObj.login('***********@gmail.com','*********')
imapObj.select_folder('INBOX', readonly=True)

UIDs = imapObj.search(['SINCE 07-Jul-2016'])

for item in UIDs:
    rawMessages = imapObj.fetch(item, ['BODY[]', 'FLAGS'])
    message = pyzmail.PyzMessage.factory(rawMessages[item]['BODY[]'])
    message.get_subject()
    message.get_addresses('from')
    message.get_addresses('to')
    message.get_addresses('cc')
    message.get_addresses('bcc')
    message.text_part != None
    message.text_part.get_payload().decode(message.text_part.charset)
    message.html_part != None
    message.html_part.get_payload().decode(message.html_part.charset)
imapObj.logout()

但是这个过程似乎会冻结很长时间,最后我总是需要做一个键盘中断。确定它根据键盘中断输出在套接字文件中的某处冻结。

请帮助我解决这个问题,很难解决网络上的这类问题。我甚至尝试过隧道但是它没有解决我的问题或者说它变得更糟(我可能没有很好地实现它:P)如果需要,我会提供任何其他信息和输出

0 个答案:

没有答案