Advertisement
Guest User

Untitled

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