Python-如何通过代理通过POP或IMAP提取电子邮件? [英] Python - How can I fetch emails via POP or IMAP through a proxy?

查看:727
本文介绍了Python-如何通过代理通过POP或IMAP提取电子邮件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

poplib或imaplib似乎都没有提供代理支持,尽管我做了google-fu尝试,但找不到很多有关它的信息.

Neither poplib or imaplib seem to offer proxy support and I couldn't find much info about it despite my google-fu attempts.

我正在使用python从各种启用了imap/pop的服务器上获取电子邮件,并且需要能够通过代理进行处理.

I'm using python to fetch emails from various imap/pop enabled servers and need to be able to do it through proxies.

理想情况下,我想能够直接在python中进行操作,但是如果我找不到任何东西,可以使用包装器(外部程序/脚本,基于OSX)强制所有流量通过代理进行处理更好.

Ideally, I'd like to be able to do it in python directly but using a wrapper (external program/script, OSX based) to force all traffic to go through the proxy might be enough if I can't find anything better.

有人可以帮我吗?我无法想象我是唯一需要通过python中的代理来获取电子邮件的人...

Could anyone give me a hand? I can't imagine I'm the only one who ever needed to fetch emails through a proxy in python...

**编辑标题以删除HTTP,因为我累的时候不应该输入太快,所以对不起**

** EDIT Title edit to remove HTTP, because I shouldn't type so fast when I'm tired, sorry for that guys **

我打算使用的代理服务器除了http之外还允许袜子.

The proxies I'm planning to use allow socks in addition to http.

Pop或Imap通过http进行工作没有什么意义(有状态与无状态),但是我的理解是,袜子可以让我做自己想做的事.

Pop or Imap work through http wouldn't make much sense (stateful vs stateless) but my understanding is that socks would allow me to do what I want.

到目前为止,实现我想要的唯一方法似乎是对imaplib的恶意入侵...如果可以的话,宁愿避免使用它.

So far the only way to achieve what I want seems to be dirty hacking of imaplib... would rather avoid it if I can.

推荐答案

您无需费心地破解imaplib.您可以尝试使用SocksiPy软件包,该软件包支持socks4,socks5和http代理(连接):

You don't need to dirtily hack imaplib. You could try using the SocksiPy package, which supports socks4, socks5 and http proxy (connect):

类似这样的事情,显然,您希望通过自定义__init__方法的额外参数等来更好地处理setproxy选项.

Something like this, obviously you'd want to handle the setproxy options better, via extra arguments to a custom __init__ method, etc.

from imaplib import IMAP4, IMAP4_SSL, IMAP4_PORT, IMAP4_SSL_PORT
from socks import sockssocket, PROXY_TYPE_SOCKS4, PROXY_TYPE_SOCKS5, PROXY_TYPE_HTTP

class SocksIMAP4(IMAP4):
    def open(self,host,port=IMAP4_PORT):
        self.host = host
        self.port = port
        self.sock = sockssocket()
        self.sock.setproxy(PROXY_TYPE_SOCKS5,'socks.example.com')
        self.sock.connect((host,port))
        self.file = self.sock.makefile('rb')

您可以对IMAP4_SSL进行类似的操作.只需将其包装到ssl套接字中即可

You could do similar with IMAP4_SSL. Just take care to wrap it into an ssl socket

import ssl

class SocksIMAP4SSL(IMAP4_SSL):
    def open(self, host, port=IMAP4_SSL_PORT):
        self.host = host
        self.port = port
        #actual privoxy default setting, but as said, you may want to parameterize it
        self.sock = create_connection((host, port), PROXY_TYPE_HTTP, "127.0.0.1", 8118)
        self.sslobj = ssl.wrap_socket(self.sock, self.keyfile, self.certfile)
        self.file = self.sslobj.makefile('rb')

这篇关于Python-如何通过代理通过POP或IMAP提取电子邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆