首页 > 解决方案 > How to parse credential args to a function handling imaplib protocols

问题描述

I have a list of emails(mine) that I want to test against a list of passwords(All valid and some none valid of course) using imaplib library. Whenever I test the program ordinarily like in the code below, it works perfectly no errors.

import sys
import imaplib

# connect to host using SSL
imap_host = 'imap.server.com'
imap_port = '993'
imap_user = 'username@email'
imap_pass = 'RightPassword'

imap = imaplib.IMAP4_SSL(imap_host, imap_port)

## login to server

try:
    login = imap.login(imap_user, imap_pass)

    if login:
        print login
except imaplib.IMAP4.error as error:
    print error

#

But whenever I run the code such as to parsing credentials through a function to handle the authentication protocols such as the following code below, I get an error saying

"LOGIN command error: BAD ['Missing \'"\'']".

I have tried all sort of things I could find using google and non seem to handle it properly.

"""
    E-mail Tester
    NB: This is for educational purpose only.
                            """

import sys
import imaplib


EMAILS_FILE = open('email_list.txt', 'r')

PASSWORD_FILE = open('pass_list.txt', 'r')

SUCCESS_FILE = open('success.txt', 'a')

EMAILS_FILE_LIST = []


def set_check(_emails):

    email = str(_emails)

    PASSWORD_FILE.seek(0)

    for passwords in PASSWORD_FILE:

        password = str(passwords)

        # connect to host using SSL
        imap_host = 'imap.server.com'
        imap_port = '993'
        imap = imaplib.IMAP4_SSL(imap_host, imap_port)

        ## login to server

        try:
            # print "%s%s" % (email,password)
            # print "I got here so far"
            # sys.exit()

            print "Testing <--> E-mail: %s - Password: %s" % (email, password)

            login = imap.login("%s","%s" % (email, password))

            if login:
                print login
                print "OK <---> E-mail: %s\nPassword: %s" % (email, password) 
        except imaplib.IMAP4.error as error:
            print error

for emails in EMAILS_FILE:
    EMAILS_FILE_LIST.append(emails)

for email_count in range(0, len(EMAILS_FILE_LIST)):
    set_check(EMAILS_FILE_LIST[email_count])

I have tried all kind of suggestions I could find on the internet but non has worked thus far.

I expect imap.login to handle the authentication without the mysterious error output

"LOGIN command error: BAD ['Missing \'"\'']"

标签: pythonpython-2.7python-multithreadingimaplib

解决方案


login = imap.login("%s","%s" % (email, password))

不起作用。它在 Python: 中引发错误TypeError: not all arguments converted during string formatting,因为您向一个 %s 提供了两个字符串。

你为什么不直接使用imap.login(email, password)?它与您尝试执行的操作具有相同的效果。

你的密码文件是什么样的?它实际上发送的是什么?请在崩溃之前提供日志行。(如有必要,匿名,但留下任何标点符号以帮助诊断)


推荐阅读