以下是我當前用於連接到端口587上gmail的smtp服務器的代碼。發出STARTTLS命令後,我將如何完成TLS會話的協商並開始發出諸如AUTH LOGIN和MAIL FROM之類的命令?我已經省略了我的Base64編碼的gmail用戶名,並在我的代碼底部附近用xxxxxxxx取代了它。如何在Python中的活動連接上啓動TLS?
從這個程序我的輸出,因爲它是是:
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
爲什麼不直接使用http://docs.python.org/library/smtplib.html? –
@MK。我想以手動方式做這個練習。 – ripit
我明白了。如果不使用用C編寫的ssl模塊,那麼在某個地方就有一個純python ssl模塊。可能從ssl.wrap_socket開始(self.sock,keyfile,certfile) – swang