Search code examples
pythonpoplib

If I give a POP server invalid credentials using poplib, why can't I try again with the correct ones?


I'm writing a script to login to Hotmail. It asks the user to enter a password and if they get the password right, all is well.

If they get the password wrong the first time however, the valid password will not work on subsequent attempts.

This is what I'm using for the user to enter their password:

import poplib

M = poplib.POP3_SSL('pop3.live.com', 995) #Connect to hotmail pop3 server
M.set_debuglevel(2)
success = False;
user = "[email protected]"

while success == False:
    try:
        password = raw_input("password: ")
        M.user(user)
        M.pass_(password)
    except:
        print "Invalid credentials"
    else:
        print "Successful login"
        success = True

Here is the output with debug level 2 on:

$ python my_program.py
password: password
*cmd* 'USER [email protected]'
*put* 'USER [email protected]'
*get* '+OK password required\r\n'
*resp* '+OK password required'
*cmd* "PASS password"
*put* "PASS password"
*get* '+OK mailbox has 1 messages\r\n'
*resp* '+OK mailbox has 1 messages'
Successful login

$ python my_program.py
password: 1234
*cmd* 'USER [email protected]'
*put* 'USER [email protected]'
*get* '+OK password required\r\n'
*resp* '+OK password required'
*cmd* 'PASS 1234'
*put* 'PASS 1234'
*get* '-ERR authentication failed\r\n'
*resp* '-ERR authentication failed'
Invalid credentials
password: password
*cmd* 'USER [email protected]'
*put* 'USER [email protected]'
*get* '+OK password required\r\n'
*resp* '+OK password required'
*cmd* "PASS password"
*put* "PASS password"
Invalid credentials

What am I missing here? I should note that I'm very new to python.


Solution

  • Okay, I guess you only get one login attempt with an object from poplib. The solution was to move the declaration of "M" down into the while loop (I swear I tried that before posting, but I must have stuffed it up somehow).