gpt4 book ai didi

python - 如何连接 Python IMAP4_SSL 和自签名服务器 SSL 证书?

转载 作者:行者123 更新时间:2023-11-28 16:36:39 25 4
gpt4 key购买 nike

我正在使用 python3 和 imaplib 连接到 Exchange 服务器。 Exchange 服务器使用为主机名“my.server.fqdn”创建的自签名证书。在我的工作站 (Ubuntu 14.04) 上进行了最近的操作系统升级后,我的 python 脚本不再有效。

我使用的基本代码片段在这里:

#!/usr/bin/python3

import imaplib
import socket
import ssl
import getpass

passwd = getpass.getpass()
mail = imaplib.IMAP4_SSL('my.server.fqdn', 1143)
mail.login('mike', passwd)
mail.list()
mail.select("INBOX")

代码在升级前运行良好。现在这段代码产生了这个错误:

Traceback (most recent call last):
File "./example.py", line 9, in <module>
mail = imaplib.IMAP4_SSL('my.server.fqdn', 1143)
File "/usr/lib/python3.4/imaplib.py", line 1222, in __init__
IMAP4.__init__(self, host, port)
File "/usr/lib/python3.4/imaplib.py", line 181, in __init__
self.open(host, port)
File "/usr/lib/python3.4/imaplib.py", line 1236, in open
IMAP4.open(self, host, port)
File "/usr/lib/python3.4/imaplib.py", line 257, in open
self.sock = self._create_socket()
File "/usr/lib/python3.4/imaplib.py", line 1228, in _create_socket
server_hostname=server_hostname)
File "/usr/lib/python3.4/ssl.py", line 364, in wrap_socket
_context=self)
File "/usr/lib/python3.4/ssl.py", line 578, in __init__
self.do_handshake()
File "/usr/lib/python3.4/ssl.py", line 805, in do_handshake
self._sslobj.do_handshake()
ssl.SSLError: [SSL: TLSV1_ALERT_INTERNAL_ERROR] tlsv1 alert internal error (_ssl.c:598)

如果 python 的自签名证书、SSL 版本有问题,或者 python 3 是否有新的东西不适用于我的旧代码,对我来说并不明显。

我确实有一个来自 Exchange 服务器的 PEM 文件,它允许我使用 curl 连接到服务器。身份验证失败,但是 curl 在 SSL 握手期间所做的检查似乎工作正常。我必须提供 curl --sslv3 选项以及 PEM 文件的路径才能正常工作。如果这就是我需要用 python 做的才能让它再次工作,我该如何为 python 提供相同的信息?

curl session 在这里:

$ curl -vvv --sslv3 https://my.server.fqdn:1143 --cacert ~/my_server_fqdn.pem 
* Rebuilt URL to: https://my.server.fqdn:1143/
* Hostname was NOT found in DNS cache
* Trying 10.10.10.10...
* Connected to my.server.fqdn (10.10.10.10) port 1143 (#0)
* successfully set certificate verify locations:
* CAfile: /home/username/my_server_fqdn.pem
CApath: /etc/ssl/certs
* SSLv3, TLS handshake, Client hello (1):
* SSLv3, TLS handshake, Server hello (2):
* SSLv3, TLS handshake, CERT (11):
* SSLv3, TLS handshake, Server key exchange (12):
* SSLv3, TLS handshake, Server finished (14):
* SSLv3, TLS handshake, Client key exchange (16):
* SSLv3, TLS change cipher, Client hello (1):
* SSLv3, TLS handshake, Finished (20):
* SSLv3, TLS change cipher, Client hello (1):
* SSLv3, TLS handshake, Finished (20):
* SSL connection using ECDHE-RSA-AES256-SHA
* Server certificate:
* subject: O=fqdn; O=SERVER; OU=M; CN=my.server.fqdn
* start date: 2013-07-29 20:59:04 GMT
* expire date: 2023-07-27 20:59:04 GMT
* common name: my.server.fqdn (matched)
* issuer: O=fqdn; O=SERVER; OU=M; CN=my.server.fqdn
* SSL certificate verify ok.
> GET / HTTP/1.1
> User-Agent: curl/7.35.0
> Host: my.server.fqdn:1143
> Accept: */*
>
* OK [CAPABILITY IMAP4REV1 AUTH=LOGIN MOVE] IMAP4rev1 server ready
GET BAD command authentication required
User-Agent: BAD command authentication required
Host: BAD command authentication required
Accept: BAD command authentication required
* SSLv3, TLS alert, Client hello (1):
* Connection #0 to host my.server.fqdn left intact

最佳答案

显然这个问题是由一个或多个 versions of TLS protocol being disabled 引起的在 Ubuntu 12.04 和 14.04 上的 openssl 包中。我最终在 python3 中使用了 ssl 上下文并强制使用 SSL 版本 3 进行连接。这似乎现在有效。真的希望这能帮助任何遇到同样问题的人。

#!/usr/bin/python3

import imaplib
import socket
import ssl
import getpass

ctx = ssl.SSLContext(ssl.PROTOCOL_SSLv3)
passwd = getpass.getpass()
mail = imaplib.IMAP4_SSL('my.server.fqdn', 1143, ssl_context=ctx)
mail.login('mike', passwd)
mail.select('INBOX', 1)
typ, uids = mail.uid("SEARCH", None, "ALL")

uids = uids[0].split()
for u in uids:
print ("found uid " , u)

mail.close()
mail.logout()
del mail

关于python - 如何连接 Python IMAP4_SSL 和自签名服务器 SSL 证书?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25318012/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com