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

查看:20
本文介绍了如何通过代理通过 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...

** EDIT Title 编辑删除 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.您可以尝试使用支持socks4、socks5和http代理(连接)的SocksiPy包:

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

像这样的事情,显然你想更好地处理 setproxy 选项,通过自定义 __init__ 方法的额外参数等.

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')

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

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