Eth*_*lis 8 python sockets ssl smtp
以下是我在端口587上连接到gmail的smtp服务器的当前代码.发出STARTTLS命令后,我将如何完成协商TLS会话并开始发出命令,如AUTH LOGIN和MAIL FROM?我已经省略了我的Base64编码的gmail用户名,并将其替换为我的代码底部附近的xxxxxxxx.
我这个程序的输出是:
220 mx.google.com ESMTP y10sm3296641yhd.6
250-mx.google.com为您服务,[75.66.47.144]
250-SIZE 35882577
250-8BITMIME
250-STARTTLS
250 ENHANCEDSTATUSCODES
220 2.0.0准备启动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
Run Code Online (Sandbox Code Playgroud)
tMC*_*tMC 11
您可以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
Run Code Online (Sandbox Code Playgroud)
有关用户名/密码编码的信息,请查看本页的AUTH LOGIN部分:http://www.samlogic.net/articles/smtp-commands-reference-auth.htm
之后,AUTH LOGIN命令已发送到服务器,服务器通过向客户端发送BASE64编码文本(问题)来请求用户名和密码."VXNlcm5hbWU6"是单词"Username"的BASE64编码文本,"UGFzc3dvcmQ6"是上面示例中单词"Password"的BASE64编码文本.客户端也使用BASE64编码发送用户名和密码.在上面的示例中,"adlxdkej"是BASE64编码的用户名,"lkujsefxlj"是BASE64编码的密码.
| 归档时间: |
|
| 查看次数: |
9630 次 |
| 最近记录: |