Advertisement
Guest User

Untitled

a guest
Mar 8th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.46 KB | None | 0 0
  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. import argparse as ap
  6. import getpass as gp
  7.  
  8. #Get sender_email and recipient_email as arguments to the program
  9. parser = ap.ArgumentParser(description='A test SMTP client without authentication')
  10. parser.add_argument('-f', '--from', dest='fromMail', required=True, metavar='<sender_email>')
  11. parser.add_argument('-t', '--to', dest='toMail', required=True, metavar='<recipient_email>')
  12. #If using the authentication of the SMTP server, also get a valid username (optional exercise)
  13. #parser.add_argument('-u', '--username', dest='username', required=True, metavar='<username>')
  14.  
  15. args = parser.parse_args()
  16. fromMail = args.fromMail #Sender's email address
  17. toMail = args.toMail #Recipient's email address
  18. #username = args.username #SMTP username in case you are implementing the optional exercise
  19.  
  20. #If using the authentication of the SMTP server, ask for a valid password (optional exercise)
  21. #password = gp.getpass(prompt='Password: ')
  22.  
  23. # Message to send
  24. msg = "\r\n I love computer networks!"
  25. endmsg = "\r\n.\r\n"
  26.  
  27. # Our mail server is smtp.stud.ntnu.no but it allows only authenticated communications. (optional exercise)
  28. #mailserver = 'smtp.stud.ntnu.no'
  29. # You can run a local simple SMTP server such as "Fake SMTP Server" and communicate with it without authentication.
  30.  
  31. # Create socket called clientSocket and establish a TCP connection
  32. # (use the appropriate port) with mailserver
  33. #Fill in start
  34. mailserver = ('', 8000)
  35. clientSocket = socket(AF_INET, SOCK_STREAM)
  36. clientSocket.connect(mailserver)
  37.  
  38. #Fill in end
  39.  
  40. recv = clientSocket.recv(1024)
  41. print(recv.decode())
  42. if recv[:3] != '220'.encode():
  43.     print('220 reply not received from server.')
  44.  
  45. # Send HELO command and print server response.
  46. # Can use EHLO instead since HELO is obsolete, but the latter can still be used
  47. heloCommand = 'EHLO Alice\r\n'
  48. clientSocket.send(heloCommand.encode()) #Python 3
  49. #clientSocket.send(heloCommand) #Python 2.7
  50. recv1 = clientSocket.recv(1024)
  51. print(recv1.decode())
  52. if recv1[:3] != '250'.encode():
  53.     print('250 reply not received from server.')
  54.  
  55. # Send MAIL FROM command and print server response.
  56. # Fill in start
  57. clientSocket.send(('MAIL FROM:' + '<' + fromMail + '>' + '\r\n').encode())
  58. recv2 = clientSocket.recv(1024)
  59. print('After MAIL FROM command: ', recv2.decode())
  60. # Fill in end
  61.  
  62. # Send RCPT TO command and print server response.
  63. # Fill in start
  64. clientSocket.send(('RCPT TO:' + '<' + toMail + '>' + '\r\n').encode())
  65. recv3 = clientSocket.recv(1024)
  66. print('After RCPT TO command: ', recv3.decode())
  67. # Fill in end
  68.  
  69. # Send DATA command and print server response.
  70. # Fill in start
  71. clientSocket.send('DATA\r\n'.encode())
  72. recv4 = clientSocket.recv(1024)
  73. print('After DATA command: ', recv4.decode())
  74. # Fill in end
  75.  
  76. # Send message data.
  77. # Fill in start
  78. clientSocket.send(msg.encode())
  79. clientSocket.send(endmsg.encode())
  80. # Fill in end
  81.  
  82. # Message ends with a single period.
  83. # Fill in start
  84. clientSocket.send('.'.encode())
  85. recv5 = clientSocket.recv(1024)
  86. print(recv5.decode())
  87. # Fill in end
  88.  
  89. # Send QUIT command and get server response.
  90. # Fill in start
  91. quitCommand = 'QUIT\r\n'
  92. clientSocket.send(quitCommand.encode())
  93. recv6 = clientSocket.recv(1024)
  94. print(recv6.decode())
  95. clientSocket.close()
  96. # Fill in end
  97.  
  98. #Note that there are more communications if you implement the optional exercise.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement