python - 如何在 python 中的事件连接上启动 TLS?

标签 python sockets smtp ssl

以下是我在端口 587 上连接到 gmail 的 smtp 服务器的当前代码。发出 STARTTLS 命令后,我将如何完成 TLS session 协商并开始发出诸如 AUTH LOGIN 和 MAIL FROM 之类的命令?我省略了我的 Base64 编码的 gmail 用户名,并将其替换为代码底部附近的 xxxxxxxx。

我从这个程序中得到的输出是:

220 mx.google.com ESMTP y10sm3296641yhd.6

250-mx.google.com at your service, [75.66.47.144]

250-SIZE 35882577

250-8BITMIME

250-STARTTLS

250 ENHANCEDSTATUSCODES

220 2.0.0 Ready to start TLS

from socket import *
import ssl
msg = "\r\n smtp.."
endmsg = "\r\n.\r\n"

# Mailserver hostname and port to be used.
mailserver = ("smtp.gmail.com", 587)


# Create a socket and create an active TCP connection with the mailserver
clientSocket = socket(AF_INET, SOCK_STREAM);
clientSocket.connect(mailserver)

# Read server response
recv = clientSocket.recv(1024)
print recv
if recv[:3] != '220':
    print '220 reply not received from server.'

# Send EHLO command and print server response.
ehloCommand = 'EHLO smtp.google.com\r\n'
clientSocket.send(ehloCommand)

recv1 = clientSocket.recv(1024)
print recv1
if recv1[:3] != '250':
    print '250 reply not received from server.'

# Send STARTTLS command to server and print server response
command = "STARTTLS\r\n"
clientSocket.send(command)

recv1 = clientSocket.recv(1024)
print recv1
if recv[:3] != '220':
    print '220 reply not received from server.'

# SEND AUTH LOGIN command and Base64 encoded username
command = "AUTH LOGIN xxxxxxxxxxxxx\r\n"
clientSocket.send(command)

recv1 = clientSocket.recv(1024)
print recv1

最佳答案

您可以 ssl 包装连接的套接字。这会给你一个想法:

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

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.

关于python - 如何在 python 中的事件连接上启动 TLS?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12593944/

相关文章:

安全关键应用程序的 Python 编码标准

python - 使用 Mechanize 和漂亮的汤在 python 中抓取原始 HTML 与 DOM

node.js - golang tcp socket - 处理多条消息

c++ - 什么是 sa_family_t

laravel - 如何在邮件 Laravel 中附加存储中的图像

python - 升级到 Python 3 后是否应该手动安装所有系统范围的软件包?

python 反转/转置字典

java - 如何在我自己的服务器上配置推送通知?

email - Liferay 6.1.20EE如何通过MailServiceUtil检查E-Mail是否成功发送

c++ - 如何用C++发送邮件