且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在python中的活动连接上启动TLS?

更新时间:2023-02-06 22:39:13

您可以 ssl 包装已连接的套接字.这会给你的想法:

You can ssl wrap a connected socket. This will give you the idea:

import ssl
import base64
from socket import *


cc = socket(AF_INET, SOCK_STREAM)
cc.connect(("smtp.gmail.com", 587))
# cc.read(..)

cc.send('helo tester.com\r\n')
cc.send('starttls\r\n')
# cc.read(..) If the server responds ok to starttls
#             tls negotiation needs to happen and all
#             communication is then over the SSL socket 

scc = ssl.wrap_socket(cc, ssl_version=ssl.PROTOCOL_SSLv23)
scc.send('auth login\r\n')
# scc.read(..)

scc.send(base64.b64encode('username')+'\r\n')
scc.send(base64.b64encode('password')+'\r\n')

# css.send(
#  mail from:
#  rcpt to:
#  data
#  etc

查看此页面的 AUTH LOGIN 部​​分以了解有关用户名/密码编码的信息:http://www.samlogic.net/articles/smtp-commands-reference-auth.htm

look at the AUTH LOGIN section of this page for info about the username/password encoding: http://www.samlogic.net/articles/smtp-commands-reference-auth.htm

在将 AUTH LOGIN 命令发送到服务器之后,服务器通过发送 BASE64 编码的文本来询问用户名和密码(问题)给客户.VXNlcm5hbWU6"是BASE64编码的文本对于单词用户名"和UGFzc3dvcmQ6"是 BASE64 编码的文本对于上例中的密码"一词.客户端发送用户名和密码也使用 BASE64 编码.adlxdkej",在上面的例子,是一个 BASE64 编码的用户名,lkujsefxlj"是一个BASE64 编码的密码.

After that the AUTH LOGIN command has been sent to the server, the server asks for username and password by sending BASE64 encoded text (questions) to the client. "VXNlcm5hbWU6" is the BASE64 encoded text for the word "Username" and "UGFzc3dvcmQ6" is the BASE64 encoded text for the word "Password" in the example above. The client sends username and password also using BASE64 encoding. "adlxdkej", in the example above, is a BASE64 encoded username and "lkujsefxlj" is a BASE64 encoded password.