Computer Networking a Top-Down Approach About Lab 3 SMTP Lab

Computer Networking: a Top-Down Approach About Lab 3: SMTP Lab

from socket import *
import ssl
import sys
import base64

# sender & reciever
sent_from = 'example@gmail.com'
sent_password = '' # One of your App passwords
sent_to = 'example@gmail.com'

# Mail content
msg = 'I love computer networks!'
info = (("From: %s\r\n"+
        "To: %s\r\n"+
        "Subject:labs\r\n"+
        "%s") % (sent_from,sent_to,msg))
endmsg = "\r\n.\r\n"

# Choose a mail server (e.g. Google mail server) and call it mailserver
mailserver = 'smtp.gmail.com'

# Create socket called clientSocket and establish a TCP connection with mailserver
clientSocket = socket(AF_INET, SOCK_STREAM)
clientSocket.connect((mailserver, 587))
recv = clientSocket.recv(1024).decode()
print(recv)
if recv[:3] != '220':
    print('220 reply not received from server.')
    sys.exit(0)
print('establish success')

# Send HELO command and print server response.
tlsComman = 'HELO Alice\r\n'
clientSocket.sendall(tlsComman.encode())
recv = clientSocket.recv(1024).decode()
print(recv)
if recv[:3] != '250':
    print('220 reply not received from server.')
    sys.exit(0)
print("connect success")

# TSL handshake
tlsComman = 'STARTTLS \r\n'
clientSocket.sendall(tlsComman.encode())
recv = clientSocket.recv(1024).decode()
if recv[:3] != '220':
    print('220 reply not received from server.')
    sys.exit(0)
context = ssl.create_default_context()
clientSocket = context.wrap_socket(clientSocket, server_hostname=mailserver)
print('TSL AUTH success')

# Auth
s="\0%s\0%s" % (sent_from, sent_password)
responce = base64.b64encode(s.encode()).decode()
clientSocket.sendall(('AUTH PLAIN %s' % responce +'\r\n').encode())
recv = clientSocket.recv(1024).decode()
if (recv[:3] == '503'):
    print('already authentication ')
if (recv[:3] != '235'):
    print('235 reply not rece9ived from server.')
    sys.exit(0)
print('ACCOUNT AUTH success')

# Send MAIL FROM command and priscnt server response.
clientSocket.sendall(('MAIL FROM: <' + sent_from + '>\r\n').encode())
recv = clientSocket.recv(1024).decode()
print(recv)
if (recv[:3] != '250'):
    print('250 reply not received from server')
    sys.exit(0)

# Send RCPT TO command and print server response.
clientSocket.sendall(('RCPT TO: <' + sent_to + '>\r\n').encode())
recv = clientSocket.recv(1024).decode()
print(recv)
if (recv[:3] != '250'):
    print('250 reply not received from server')
    sys.exit(0)

# Send DATA command and print server response.
dataCommand = 'DATA\r\n'
clientSocket.send(dataCommand.encode())
recv = clientSocket.recv(1024).decode()
print(recv)
if (recv[:3] != '354'):
    print('354 reply not received from server')
    sys.exit()

# Send message data.
clientSocket.sendall(info.encode())

# msg ends with a single period.
clientSocket.sendall(endmsg.encode())
recv = clientSocket.recv(1024).decode()
print(recv)
if (recv[:3] != '250'):
    print('250 reply not received from server')

# Send QUIT command and get server response.
clientSocket.sendall('QUIT\r\n'.encode())
recv = clientSocket.recv(1024).decode()
print (recv)
if recv[:3] != '221':
	print ('221 reply not received from server.')

# Close connection
clientSocket.close()
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值