experiments

All kinds of coding experiments
Log | Files | Refs | Submodules

SMTPClient_inspiration.py (1995B)


      1 # This skeleton is valid for both Python 2.7 and Python 3.
      2 # You should be aware of your additional code for compatibility of the Python version of your choice.
      3 
      4 from socket import *
      5 
      6 # Message to send
      7 msg = "\r\n I love computer networks!"
      8 endmsg = "\r\n.\r\n"
      9 
     10 # Our mail server is smtp.stud.ntnu.no
     11 mailserver = 'smtp.stud.ntnu.no'
     12 
     13 # Create socket called clientSocket and establish a TCP connection
     14 # (use the appropriate port) with mailserver
     15 #Fill in start
     16 clientSocket = socket(AF_INET, SOCK_STREAM)
     17 clientSocket.connect((mailserver, 25))   # 25 is the standard SMTP port
     18 #Fill in end
     19 
     20 recv = clientSocket.recv(1024)
     21 print(recv)
     22 if recv[:3] != '220':
     23     print('220 reply not received from server.'.encode('utf-8'))
     24 
     25 # Send HELO command and print server response.
     26 heloCommand = 'HELO Alice\r\n'
     27 clientSocket.send(heloCommand.encode('utf-8'))
     28 recv1 = clientSocket.recv(1024)
     29 print(recv1.decode('utf-8'))
     30 
     31 if recv1[:3] != '250':
     32     print('250 reply not received from server.')
     33 
     34 # Send MAIL FROM command and print server response.
     35 # Fill in start
     36 clientSocket.send('MAIL FROM: <vetlehaf@stud.ntnu.no>\r\n'.encode('utf-8'))
     37 recv2 = clientSocket.recv(1024)
     38 print(recv2.decode('utf-8'))
     39 # Fill in end
     40 
     41 # Send RCPT TO command and print server response.
     42 # Fill in start
     43 clientSocket.send('RCPT TO: <vetlehaf@stud.ntnu.no>\r\n'.encode('utf-8'))
     44 recv3 = clientSocket.recv(1024)
     45 print(recv3.decode('utf-8'))
     46 # Fill in end
     47 
     48 # Send DATA command and print server response.
     49 # Fill in start
     50 clientSocket.send('DATA\r\n'.encode('utf-8'))
     51 recv4 = clientSocket.recv(1024)
     52 print(recv4.decode('utf-8'))
     53 # Fill in end
     54 
     55 # Send message data.
     56 # Fill in start
     57 clientSocket.send(msg.encode('utf-8'))
     58 # Fill in end
     59 
     60 # Message ends with a single period.
     61 # Fill in start
     62 clientSocket.send(endmsg.encode('utf-8'))
     63 # Fill in end
     64 
     65 # Send QUIT command and get server response.
     66 # Fill in start
     67 clientSocket.send('QUIT\r\n'.encode('utf-8'))
     68 # Fill in end