SSL证书适用于Python 2.6.6的smtplib,但不适用于Python 2.7.9

时间:2015-05-20 15:41:58

标签: python ssl smtplib

代码是:

import smtplib

SERVER = XXX
PORT = 587
USERNAME = YYY
PASSWORD = ZZZZ
SENDER = 'foo@domain.com'
RECEIVERS = ['bar@anotherdomain.com', ]

CERTFILE = "/path/to/certfile.crt"
KEYFILE = "/path/to/certfile.key"

client = smtplib.SMTP(SERVER, PORT)
client.set_debuglevel(1)
client.ehlo()
client.starttls(keyfile=KEYFILE, certfile=CERTFILE)
client.ehlo()
client.login(USERNAME, PASSWORD)
client.sendmail(SENDER, RECEIVERS, """Subject: XXX

hello world!""")

当我使用python 2.6.6时它工作(发送电子邮件)但是当我使用Python 2.7.9时它给出的错误是:

[...]
send: 'STARTTLS\r\n'
reply: '220 2.0.0 SMTP server ready\r\n'
reply: retcode (220); Msg: 2.0.0 SMTP server ready
Traceback (most recent call last):
  File "./fbo_test_email.py", line 31, in <module>
    client.starttls(keyfile=KEYFILE, certfile=CERTFILE)
  File "/usr/local/lib/python2.7/smtplib.py", line 649, in starttls
    self.sock = ssl.wrap_socket(self.sock, keyfile, certfile)
  File "/usr/local/lib/python2.7/ssl.py", line 891, in wrap_socket
    ciphers=ciphers)
  File "/usr/local/lib/python2.7/ssl.py", line 509, in __init__
    self._context.load_cert_chain(certfile, keyfile)
ssl.SSLError: [X509: KEY_VALUES_MISMATCH] key values mismatch (_ssl.c:2525)

更多信息:

  • crt文件是RSA私钥,第一行是:----- BEGIN RSA PRIVATE KEY -----
  • 密钥文件的第一行是: ----- BEGIN CERTIFICATE -----

我无法理解为什么证书适用于Python 2.6.6的smtplib但不适用于Python 2.7.9。

任何提示都会受到赞赏,提前谢谢。

0 个答案:

没有答案