简体   繁体   English

如何通过代理通过 POP 或 IMAP 获取电子邮件?

[英]How can I fetch emails via POP or IMAP through a proxy?

Neither poplib or imaplib seem to offer proxy support and I couldn't find much info about it despite my google-fu attempts. poplib 或 imaplib 似乎都没有提供代理支持,尽管我尝试了 google-fu,但我找不到太多关于它的信息。

I'm using python to fetch emails from various imap/pop enabled servers and need to be able to do it through proxies.我正在使用 python 从各种启用 imap/pop 的服务器获取电子邮件,并且需要能够通过代理来完成。

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 中执行此操作,但使用包装器(外部程序/脚本,基于 OSX)强制所有流量通过代理到 go 如果我找不到更好的东西可能就足够了。

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...我无法想象我是唯一需要通过 python 中的代理获取电子邮件的人......

** 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.除了 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. Pop 或 Imap 通过 http 工作没有多大意义(有状态与无状态),但我的理解是袜子可以让我做我想做的事。

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 进行肮脏的黑客攻击......如果可以的话,宁愿避免它。

You don't need to dirtily hack imaplib.你不需要肮脏地破解 imaplib。 You could try using the SocksiPy package, which supports socks4, socks5 and http proxy (connect):你可以尝试使用支持socks4、socks5和http代理(连接)的SocksiPy包:

Something like this, obviously you'd want to handle the setproxy options better, via extra arguments to a custom __init__ method, etc.像这样的事情,显然你想更好地处理 setproxy 选项,通过自定义__init__方法的额外参数等。

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

You could do similar with IMAP4_SSL.你可以用 IMAP4_SSL 做类似的事情。 Just take care to wrap it into an ssl socket只需注意将其包装到 ssl 套接字中

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

Answer to my own question... There's a quick and dirty way to force trafic from a python script to go through a proxy without hassle using Socksipy (thanks MattH for pointing me that way)回答我自己的问题......有一种快速而肮脏的方法可以使用 Socksipy 强制来自 python 脚本的流量通过代理而无需麻烦(感谢 MattH 指出我那样)

import socks
import socket
socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS4,proxy_ip,port,True)
socket.socket = socks.socksocket

That global socket override is obviously a bit brutal, but works as a quick fix till I find the time to properly subclass IMAP4 and IMAP4_SSL.全局套接字覆盖显然有点残酷,但可以作为一个快速修复,直到我找到时间正确地对 IMAP4 和 IMAP4_SSL 进行子类化。

If I understand you correctly you're trying to put a square peg in a round hole.如果我理解正确,您是在尝试将方钉插入圆孔中。

An HTTP Proxy only knows how to "talk" HTTP so can't connect to a POP or IMAP server directly. HTTP 代理只知道如何“交谈” HTTP,因此不能直接连接到 POP 或 IMAP 服务器。

If you want to do this you'll need to implement your own server somewhere to talk to the mail servers.如果你想这样做,你需要在某处实现你自己的服务器来与邮件服务器通信。 It would receive HTTP Requests and then make the appropriate calls to the Mail Server.它将接收 HTTP 请求,然后对邮件服务器进行适当的调用。 Eg:例如:

How practical this would be I don't know since you'd have to convert a stateful protocol into a stateless one.我不知道这有多实用,因为您必须将有状态协议转换为状态协议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM