Advertisement
Guest User

Untitled

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