You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python实现带USER:PASS:HOST:PORT格式HTTP代理的IMAP邮件抓取遇阻求助

问题描述

现有IMAP邮件获取代码

我有一段通过IMAP登录邮箱获取近期邮件的Python代码:

def fetchRecentEmail(emailAddr, emailPassword, timeout=120):
    host = fetch_imap_server(emailAddr)  # e.g. 'outlook.office365.com'
    with IMAP4_SSL(host) as session:
        status, _ = session.login(emailAddr, emailPassword)
        if status == 'OK':
                # fetch most recent message
                status, messageData = session.select("Inbox")
                # ... 后续逻辑

已验证可用的代理配置

我需要给这段代码添加代理功能,使用的代理格式为USER:PASS:HOST:PORT(不同于常见的IP:PORT),且该代理已通过requests验证可用:

USER = 'Pp7fwti5n-res-any-sid-' + random8Digits()
PASS = 'abEDxts7v'
HOST = 'gw.proxy.rainproxy.io'
PORT = 5959

proxy = f'{USER}:{PASS}@{HOST}:{PORT}'

proxies = {
    'http': 'http://' + proxy,
    'https': 'http://' + proxy
}

response = requests.get(
    'https://ip.nf/me.json',
    proxies=proxies, timeout=15
)

尝试的代理实现及问题

我参考资料尝试用SocksIMAP4SSL实现代理连接,需要在两处添加timeout参数才能运行:

def _create_socket(self, timeout=None):
    sock = SocksIMAP4._create_socket(self, timeout)
    server_hostname = self.host if ssl.HAS_SNI else None
    return self.ssl_context.wrap_socket(
        sock, server_hostname=server_hostname
    )

def open(self, host='', port=IMAP4_PORT, timeout=None):
    SocksIMAP4.open(self, host, port, timeout)

但仍遇到问题:

  • 当IMAP使用非443端口时,报错:

GeneralProxyError: Socket error: 403: Forbidden
[*] Note: The HTTP proxy server may not be supported by PySocks (must be a CONNECT tunnel proxy)

  • 若使用443端口,代码执行到mailbox = SocksIMAP4SSL(时无响应,无法继续执行。

多进程相关疑问

我计划在2个CPU核心上同时运行这段代码,有两个疑问:

  • 使用443端口是否会导致系统其他进程无法使用该端口?
  • 多进程间是否会产生端口冲突?

内容的提问来源于stack exchange,提问作者P i

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.22 22:48:52