首页 \ 问答 \ imaplib.error:命令SEARCH在状态AUTH中非法,只允许在SELECTED状态(imaplib.error: command SEARCH illegal in state AUTH, only allowed in states SELECTED)

imaplib.error:命令SEARCH在状态AUTH中非法,只允许在SELECTED状态(imaplib.error: command SEARCH illegal in state AUTH, only allowed in states SELECTED)

def connect_imap():
    m = imaplib.IMAP4_SSL("imap.gmail.com", 993)
    print("{0} Connecting to mailbox via IMAP...".format(datetime.datetime.today().strftime("%Y-%m-%d %H:%M:%S")))
    details = login_credentials()
    m.login(details[0], details[1])
    return m


m = connect_imap()
typ, data = m.search(None, 'ALL')
m.close()
m.logout()

上面代码的输出是:

2016-08-24 10:55:34 Connecting to mailbox via IMAP...
    Traceback (most recent call last):
      File "/home/zoikmail/Desktop/test.py", line 25, in <module>
        typ, data = m.search(None, 'ALL')
      File "/usr/lib/python2.7/imaplib.py", line 640, in search
        typ, dat = self._simple_command(name, *criteria)
      File "/usr/lib/python2.7/imaplib.py", line 1088, in _simple_command
        return self._command_complete(name, self._command(name, *args))
      File "/usr/lib/python2.7/imaplib.py", line 838, in _command
        ', '.join(Commands[name])))
    imaplib.error: command SEARCH illegal in state AUTH, only allowed in states SELECTED
    [Finished in 1.2s with exit code 1]
    [shell_cmd: python -u "/home/zoikmail/Desktop/test.py"]
    [dir: /home/zoikmail/Desktop]
    [path: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games]

上面的代码有什么问题?


def connect_imap():
    m = imaplib.IMAP4_SSL("imap.gmail.com", 993)
    print("{0} Connecting to mailbox via IMAP...".format(datetime.datetime.today().strftime("%Y-%m-%d %H:%M:%S")))
    details = login_credentials()
    m.login(details[0], details[1])
    return m


m = connect_imap()
typ, data = m.search(None, 'ALL')
m.close()
m.logout()

The Output of the above code is:

2016-08-24 10:55:34 Connecting to mailbox via IMAP...
    Traceback (most recent call last):
      File "/home/zoikmail/Desktop/test.py", line 25, in <module>
        typ, data = m.search(None, 'ALL')
      File "/usr/lib/python2.7/imaplib.py", line 640, in search
        typ, dat = self._simple_command(name, *criteria)
      File "/usr/lib/python2.7/imaplib.py", line 1088, in _simple_command
        return self._command_complete(name, self._command(name, *args))
      File "/usr/lib/python2.7/imaplib.py", line 838, in _command
        ', '.join(Commands[name])))
    imaplib.error: command SEARCH illegal in state AUTH, only allowed in states SELECTED
    [Finished in 1.2s with exit code 1]
    [shell_cmd: python -u "/home/zoikmail/Desktop/test.py"]
    [dir: /home/zoikmail/Desktop]
    [path: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games]

What's wrong in the above code?


原文:https://stackoverflow.com/questions/39115141
更新时间:2024-04-24 20:04

最满意答案

这里有一个Javascript版本,你可以传递它应该开始计算的日期,你想要知道苹果的数量的日期,一周中的苹果将被添加,一天中的更新将发生以及将在每个日期添加的苹果数量。

Date.prototype.addDays = function (days) {
    var result = new Date(this);
    result.setDate(result.getDate() + days);
    return result;
}
Date.prototype.addHours = function (hours) {
    var result = new Date(this);
    result.setHours(result.getHours() + hours);
    return result;
}

function getApples(startdate, date, updateDays, updateTime, applesPerUpdate) {
    var startDay = startdate.getDay();
    var firstUpdateDate;
    for(day of updateDays) {
        if (day >= startDay) {//assumes startdate has no time added
            firstUpdateDate = startdate.addDays(day - startDay).addHours(updateTime);
            break;
        }
    }
    if (!firstUpdateDate)
        firstUpdateDate = startdate.addDays(7 - (startDay - updateDays[0])).addHours(updateTime);

    var updateDaysReverse = updateDays.slice(0).reverse();//clones the array

    var dateDay = date.getDay();
    var lastUpdateDate;
    for(day of updateDaysReverse) {
        if (day < dateDay || day == dateDay && date.getHours() > updateTime) {
            lastUpdateDate = date.addDays(day - dateDay);
            break;
        }
    }
    if (!lastUpdateDate)
        lastUpdateDate = date.addDays(updateDaysReverse[0] - (7 + dateDay));
    lastUpdateDate = new Date(Date.UTC(1900 + lastUpdateDate.getYear(), lastUpdateDate.getMonth(), lastUpdateDate.getDate(), updateTime, 0, 0, 0));

    var secs = Math.trunc((lastUpdateDate - firstUpdateDate));
    if (secs < 0) return 0;

    var dayDiffs = [];
    for(day of updateDays)
        dayDiffs.push(day - updateDays[0]);

    var weeks = Math.trunc(secs / 604800000);
    var days = Math.trunc((secs % 604800000) / 86400000);
    var apples = weeks * updateDays.length;

    for(diff of dayDiffs)
    {
        if (diff <= days)
            apples++;
        else
            break;
    }
    return apples * applesPerUpdate;
}
// important, day and month is zero-based
var startDate = new Date(Date.UTC(2016, 01, 07, 0, 0, 0, 0));
var updateDays = [2, 4];// 0 = sunday , have to be in order and must be 0 <= x < 7
var updateTime = 9;

console.log(getApples(startDate, new Date(), updateDays, updateTime, 2));

比我更复杂,我没有多少测试,所以可能有错误。

这是一个玩弄价值观的人。


Here a Javascript version, you can pass it the date it should start counting, the date you want to know the ammount of apples of, the days of the week apples will be added, the hours of the day the updat will take place and the ammount of apples that will be added on each of these dates.

Date.prototype.addDays = function (days) {
    var result = new Date(this);
    result.setDate(result.getDate() + days);
    return result;
}
Date.prototype.addHours = function (hours) {
    var result = new Date(this);
    result.setHours(result.getHours() + hours);
    return result;
}

function getApples(startdate, date, updateDays, updateTime, applesPerUpdate) {
    var startDay = startdate.getDay();
    var firstUpdateDate;
    for(day of updateDays) {
        if (day >= startDay) {//assumes startdate has no time added
            firstUpdateDate = startdate.addDays(day - startDay).addHours(updateTime);
            break;
        }
    }
    if (!firstUpdateDate)
        firstUpdateDate = startdate.addDays(7 - (startDay - updateDays[0])).addHours(updateTime);

    var updateDaysReverse = updateDays.slice(0).reverse();//clones the array

    var dateDay = date.getDay();
    var lastUpdateDate;
    for(day of updateDaysReverse) {
        if (day < dateDay || day == dateDay && date.getHours() > updateTime) {
            lastUpdateDate = date.addDays(day - dateDay);
            break;
        }
    }
    if (!lastUpdateDate)
        lastUpdateDate = date.addDays(updateDaysReverse[0] - (7 + dateDay));
    lastUpdateDate = new Date(Date.UTC(1900 + lastUpdateDate.getYear(), lastUpdateDate.getMonth(), lastUpdateDate.getDate(), updateTime, 0, 0, 0));

    var secs = Math.trunc((lastUpdateDate - firstUpdateDate));
    if (secs < 0) return 0;

    var dayDiffs = [];
    for(day of updateDays)
        dayDiffs.push(day - updateDays[0]);

    var weeks = Math.trunc(secs / 604800000);
    var days = Math.trunc((secs % 604800000) / 86400000);
    var apples = weeks * updateDays.length;

    for(diff of dayDiffs)
    {
        if (diff <= days)
            apples++;
        else
            break;
    }
    return apples * applesPerUpdate;
}
// important, day and month is zero-based
var startDate = new Date(Date.UTC(2016, 01, 07, 0, 0, 0, 0));
var updateDays = [2, 4];// 0 = sunday , have to be in order and must be 0 <= x < 7
var updateTime = 9;

console.log(getApples(startDate, new Date(), updateDays, updateTime, 2));

Was more coplicated than I thaught, and i havn't testet it much, so there may be bugs.

Here is a plunker to play with the values.

相关问答

更多
  • 这里有一个Javascript版本,你可以传递它应该开始计算的日期,你想要知道苹果的数量的日期,一周中的苹果将被添加,一天中的更新将发生以及将在每个日期添加的苹果数量。 Date.prototype.addDays = function (days) { var result = new Date(this); result.setDate(result.getDate() + days); return result; } Date.prototype.addHours = fun ...
  • 如下 month date_of_first_Monday how_many_Mondays Ith_Monday_request the_date_request 1/1 =Choose(Weekday(A2,2),0,6,5,4,3,2,1)+A2 =roundup((day(eomonth(a2,0 ...
  • 这些数字是如何得出的? 那么,这取决于你使用的系统,但我怀疑你是在ISO-8601周数之后,这个定义如下: 2。10。10日历周号 根据一年中的第一个日历周是包括该年的第一个星期四以及日历年的最后一个日历周是紧接在该年之前的那一周的规则,在其日历年内标识日历周的序号。下一个日历年的第一个日历周 请记住,ISO-8601的一周开始于星期一,并在星期日结束 - 所以表达“第一个星期四”规则的另一种方式是,一年的第一周是包含新年至少四天的第一周。 另一个重要的一点是,当你使用“一周的一周”来表达价值时,你需要使用 ...
  • 这将为您提供每周最大gameID所有周。 返回的行将以weekNum的形式按升序排序。 SELECT weekNum, MAX(gameID) AS lastGameOfWeek FROM nflp_schedule GROUP BY weekNum ORDER BY weekNum ASC 要根据上面派生的返回的gameID获取其他值,您需要执行subselect作为过滤条件或连接。 这看起来像这样(使用subselect): SELECT gameID, weekNum, homeScore, vis ...
  • 使用cross join应该可以工作,我只是要粘贴下面的SQL Fiddle的markdown输出。 对于2013-01-08系列来说,你的样本输出似乎不正确:thisyr应该是2,而不是3.这可能不是最好的方法,但是我的Postgresql知识还有很多不足之处。 SQL小提琴 PostgreSQL 9.2.4架构设置 : CREATE TABLE Table1 ("Pt_ID" varchar(6), "exam_date" date); INSERT INTO Table1 ("Pt_ ...
  • 如果您以正确的方式考虑它, SO 1267126的答案可以应用于您的问题。 您在组中的每个错误报告日期都映射到同一周。 因此,根据定义,每个错误日期也必须映射到一周的同一个开始。 因此,您可以在错误报告日期运行“从给定日期开始的一周开始”计算,以及周数计算,并按两个(适度可怕)表达式进行分组,最后得到您寻求的答案。 SELECT DATEPART(wk, DATEADD(day, 0, DATEDIFF(d, 0, bg_reported_date))) [week], DATEADD(dd, ...
  • 您的问题有一个更简单的解决方案。 您可以使用NSCalendar的nextDateAfterDate()方法: let cal = NSCalendar.currentCalendar() let comps = NSDateComponents() comps.hour = 10 comps.weekday = cal.firstWeekday let now = NSDate() let fireDate = cal.nextDateAfterDate(now, matchingComponents: ...
  • 我面临着同样的问题。 我的解决方案 - 也许不是最好的解决方案,但它确实有效。 正如您已经提到的:大多数案例都是微不足道的,您只需知道年末/开始。 因此,我只需在下表中存储所需年数的相关周数: CREATE TABLE `NEXT_WEEK_TAB` ( `CUR_WEEK` varchar(8) NOT NULL, `NEXT_WEEK` varchar(8) NOT NULL, PRIMARY KEY (`CUR_WEEK`,`NEXT_WEEK`) ) 您现在使用当前算法获得以下一周: ...

相关文章

更多

最新问答

更多
  • 如何在Laravel 5.2中使用paginate与关系?(How to use paginate with relationships in Laravel 5.2?)
  • linux的常用命令干什么用的
  • 由于有四个新控制器,Auth刀片是否有任何变化?(Are there any changes in Auth blades due to four new controllers?)
  • 如何交换返回集中的行?(How to swap rows in a return set?)
  • 在ios 7中的UITableView部分周围绘制边界线(draw borderline around UITableView section in ios 7)
  • 使用Boost.Spirit Qi和Lex时的空白队长(Whitespace skipper when using Boost.Spirit Qi and Lex)
  • Java中的不可变类(Immutable class in Java)
  • WordPress发布查询(WordPress post query)
  • 如何在关系数据库中存储与IPv6兼容的地址(How to store IPv6-compatible address in a relational database)
  • 是否可以检查对象值的条件并返回密钥?(Is it possible to check the condition of a value of an object and JUST return the key?)
  • GEP分段错误LLVM C ++ API(GEP segmentation fault LLVM C++ API)
  • 绑定属性设置器未被调用(Bound Property Setter not getting Called)
  • linux ubuntu14.04版没有那个文件或目录
  • 如何使用JSF EL表达式在param中迭代变量(How to iterate over variable in param using JSF EL expression)
  • 是否有可能在WPF中的一个单独的进程中隔离一些控件?(Is it possible to isolate some controls in a separate process in WPF?)
  • 使用Python 2.7的MSI安装的默认安装目录是什么?(What is the default installation directory with an MSI install of Python 2.7?)
  • 寻求多次出现的表达式(Seeking for more than one occurrence of an expression)
  • ckeditor config.protectedSource不适用于editor.insertHtml上的html元素属性(ckeditor config.protectedSource dont work for html element attributes on editor.insertHtml)
  • linux只知道文件名,不知道在哪个目录,怎么找到文件所在目录
  • Actionscript:检查字符串是否包含域或子域(Actionscript: check if string contains domain or subdomain)
  • 将CouchDB与AJAX一起使用是否安全?(Is it safe to use CouchDB with AJAX?)
  • 懒惰地初始化AutoMapper(Lazily initializing AutoMapper)
  • 使用hasclass为多个div与一个按钮问题(using hasclass for multiple divs with one button Problems)
  • Windows Phone 7:检查资源是否存在(Windows Phone 7: Check If Resource Exists)
  • 无法在新线程中从FREContext调用getActivity()?(Can't call getActivity() from FREContext in a new thread?)
  • 在Alpine上升级到postgres96(/ usr / bin / pg_dump:没有这样的文件或目录)(Upgrade to postgres96 on Alpine (/usr/bin/pg_dump: No such file or directory))
  • 如何按部门显示报告(How to display a report by Department wise)
  • Facebook墙贴在需要访问令牌密钥后无法正常工作(Facebook wall post not working after access token key required)
  • Javascript - 如何在不擦除输入的情况下更改标签的innerText(Javascript - how to change innerText of label while not wiping out the input)
  • WooCommerce / WordPress - 不显示具有特定标题的产品(WooCommerce/WordPress - Products with specific titles are not displayed)