parent
0905ada462
commit
b0d9bab304
@ -1,66 +1,71 @@
|
||||
from socket import *
|
||||
from base64 import *
|
||||
|
||||
mailfrom = "mail from:2@test.com\r\n"
|
||||
rcptto = "rcpt to:1@test.com\r\n"
|
||||
data = "data\r\n"
|
||||
quitmsg = "quit\r\n"
|
||||
|
||||
msg = "\r\n I love computer networks!"
|
||||
# 填写代码使程序发送的邮件中包含内容为 "Hello World!"。
|
||||
msg = "Hello World!"
|
||||
|
||||
endmsg = "\r\n.\r\n"
|
||||
#此处使用易邮邮件服务器软件搭建了一个内网邮件服务器
|
||||
mailserver = '10.130.82.62'
|
||||
|
||||
#本地搭建的邮件服务器IP地址。
|
||||
mailserver = 'localhost'
|
||||
# 在端口25上启动邮件服务器。
|
||||
mailport = 25
|
||||
connectaddress = (mailserver, mailport)
|
||||
# Create socket called clientSocket and establish a TCP connection with mailserver
|
||||
# 创建 socket 和邮件服务器建立 TCP 连接。
|
||||
clientSocket = socket(AF_INET, SOCK_STREAM)
|
||||
clientSocket.connect(connectaddress)
|
||||
|
||||
recv = clientSocket.recv(1024)
|
||||
print (recv)
|
||||
if recv[:3] != '220':
|
||||
|
||||
if recv.decode()[:3] != '220':
|
||||
print ('220 reply not received from server.')
|
||||
# Send HELO command and print server response.
|
||||
|
||||
# 发送 HELO 命令,打印服务器响应。
|
||||
heloCommand = 'HELO Alice\r\n'
|
||||
clientSocket.send(bytes(heloCommand.encode()))
|
||||
recv1 = clientSocket.recv(1024)
|
||||
print (recv1.decode())
|
||||
if recv1[:3] != '250':
|
||||
|
||||
if recv1.decode()[:3] != '250':
|
||||
print ('250 reply not received from server.')
|
||||
|
||||
#print('000000000000000')
|
||||
#从命令和打印服务器响应发送邮件。
|
||||
|
||||
#发送登陆认证信息
|
||||
login = b'auth login\r\n'
|
||||
clientSocket.send(login)
|
||||
recv2 = clientSocket.recv(1024).decode('utf-8')
|
||||
print ('222+',recv2)
|
||||
|
||||
print ('登陆认证',recv2)
|
||||
#发送用户名
|
||||
userCommand = b64encode('2@test.com'.encode('utf-8'))
|
||||
clientSocket.send((str(userCommand,encoding='utf-8')+'\r\n').encode())
|
||||
recv3 = clientSocket.recv(1024).decode('utf-8')
|
||||
print ('333+',recv3)
|
||||
|
||||
print ('用户名',recv3)
|
||||
#发送密码
|
||||
password = b64encode('2'.encode('utf-8'))
|
||||
clientSocket.send((str(password,encoding='utf-8')+'\r\n').encode())
|
||||
recv4 = clientSocket.recv(1024).decode('utf-8')
|
||||
print ('444+',recv4)
|
||||
|
||||
#print('0000000000000000')
|
||||
print ('密码',recv4)
|
||||
|
||||
# 发送 MAIL FROM 命令,打印服务器响应。
|
||||
clientSocket.send(bytes(mailfrom.encode()))
|
||||
check = clientSocket.recv(1024)
|
||||
print(check)
|
||||
# 发送 RCPT TO 命令,打印服务器响应。
|
||||
clientSocket.send(bytes(rcptto.encode()))#将RCPT发送到命令和打印服务器响应。
|
||||
check1 = clientSocket.recv(1024)
|
||||
print(check1)
|
||||
# 发送 DATA 命令,打印服务器响应。
|
||||
clientSocket.send(bytes(data.encode()))#发送数据命令和打印服务器响应。
|
||||
check2 = clientSocket.recv(1024)
|
||||
print(check2)
|
||||
# 发送邮件内容。消息以单个"." 结束。
|
||||
clientSocket.send(bytes((mailfrom+msg+endmsg).encode()))#发送消息数据。
|
||||
check3 = clientSocket.recv(1024)
|
||||
print(check3)
|
||||
#发送退出命令并获得服务器响应。
|
||||
# 发送 QUIT 命令,获取服务器响应。
|
||||
clientSocket.send(bytes(quitmsg.encode()))
|
||||
check4 = clientSocket.recv(1024)
|
||||
print(check4)
|
||||
clientSocket.close()
|
||||
|
||||
clientSocket.close()
|
@ -1,62 +0,0 @@
|
||||
# Import socket module
|
||||
from socket import *
|
||||
import sys # In order to terminate the program
|
||||
|
||||
# Create a TCP server socket
|
||||
#(AF_INET is used for IPv4 protocols)
|
||||
#(SOCK_STREAM is used for TCP)
|
||||
|
||||
serverSocket = socket(AF_INET, SOCK_STREAM)
|
||||
|
||||
# Assign a port number
|
||||
serverPort = 80
|
||||
|
||||
# Bind the socket to server address and server port
|
||||
serverSocket.bind(("", serverPort))
|
||||
|
||||
# Listen to at most 1 connection at a time
|
||||
serverSocket.listen(1)
|
||||
|
||||
# Server should be up and running and listening to the incoming connections
|
||||
|
||||
while True:
|
||||
print('The server is ready to receive')
|
||||
|
||||
# Set up a new connection from the client
|
||||
connectionSocket, addr = serverSocket.accept()
|
||||
|
||||
# If an exception occurs during the execution of try clause
|
||||
# the rest of the clause is skipped
|
||||
# If the exception type matches the word after except
|
||||
# the except clause is executed
|
||||
try:
|
||||
# Receives the request message from the client
|
||||
message = connectionSocket.recv(1024).decode()
|
||||
# Extract the path of the requested object from the message
|
||||
# The path is the second part of HTTP header, identified by [1]
|
||||
filename = message.split()[1]
|
||||
# Because the extracted path of the HTTP request includes
|
||||
# a character '\', we read the path from the second character
|
||||
f = open(filename[1:])
|
||||
# Store the entire contenet of the requested file in a temporary buffer
|
||||
outputdata = f.read()
|
||||
# Send the HTTP response header line to the connection socket
|
||||
connectionSocket.send("HTTP/1.1 200 OK\r\n\r\n".encode())
|
||||
|
||||
# Send the content of the requested file to the connection socket
|
||||
for i in range(0, len(outputdata)):
|
||||
connectionSocket.send(outputdata[i].encode())
|
||||
connectionSocket.send("\r\n".encode())
|
||||
|
||||
# Close the client connection socket
|
||||
connectionSocket.close()
|
||||
|
||||
except IOError:
|
||||
# Send HTTP response message for file not found
|
||||
connectionSocket.send("HTTP/1.1 404 Not Found\r\n\r\n".encode())
|
||||
connectionSocket.send("<html><head></head><body><h1>404 Not Found</h1></body></html>\r\n".encode())
|
||||
# Close the client connection socket
|
||||
connectionSocket.close()
|
||||
|
||||
serverSocket.close()
|
||||
sys.exit()#Terminate the program after sending the corresponding data
|
Loading…
Reference in new issue