Search code examples
pythontelnetlib

Not Getting any responses from telnet in python


Server is connecting though. Alternatively I tried commands in putty and got the response from there but I dont know why the code is not working: Here is the snippet of it that is not working

import telnetlib
host = 'aspmx2.googlemail.com'
timeout = 5
session = telnetlib.Telnet(host, 25, timeout)
session.open(host,port ='25',timeout = timeout)
session.write("\r\n")
print(session.read_until("\n\r",10))
session.write("helo hi"+b"\r")
print(session.read_until("\n\r",10))
session.write(('MAIL FROM:someone@gmail.com').encode('ascii') + b"\r")
frommail= session.read_until(b"/r/n/r/n#>", timeout-1 )
session.write(('RCPT TO:someoneelse@gmail.com').encode('ascii') + b"\r")
check= session.read_until(b"/r/n/r/n#>", timeout-1 )
session.close()

To this I am getting the following response:

220 mx.google.com ESMTP y13si5854003pde.84 - gsmtp


[Finished in 28.1s]

Solution

  • Looks like you aren't moving to the new line when you're writing to the session.

    Your write commands could look like this: session.write("helo hi"+b"\r\n")

    OR

    session.write(('MAIL FROM:someone@gmail.com').encode('ascii') + b"\r\n")

    Hope this helps.