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
满意答案
这里有一个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.
相关问答
更多每周增加两次(Increase number twice a week)
Excel公式将月份增加一个,每周增加一个月(Excel formula to increase month by one and week in month by one)
如何根据日期计算“周数”?(How is the “week number” calculated based on a date?)
从每周编号中选择MAX(Select MAX from each week number)
每周记录总数(Total Number of Records per Week)
SQL Server - 根据周数获取一周内的第一个日期?(SQL Server - Get first date in a week, given the week number?)
NSDate来自周数(NSDate from week number)
如何计算下周的数量?(How Do I Calculate the Number of the Following Week?)
相关文章
更多solr faceted search
[翻译][Trident] Trident state原理
Riak Search
storm client command
Faceted search
Solr: a custom Search RequestHandler
Custom SOLR Search Components - 2 Dev Tricks
Haystack - Search for Django
Realtime Search: Solr vs Elasticsearch
Hadoop Namenode不能启动(dfs/name is in an inconsistent state)
最新问答
更多获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
如何通过引用返回对象?(How is returning an object by reference possible?)
矩阵如何存储在内存中?(How are matrices stored in memory?)
每个请求的Java新会话?(Java New Session For Each Request?)
css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
xcode语法颜色编码解释?(xcode syntax color coding explained?)
在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
Copyright ©2023 peixunduo.com All Rights Reserved.粤ICP备14003112号
本站部分内容来源于互联网,仅供学习和参考使用,请莫用于商业用途。如有侵犯你的版权,请联系我们(neng862121861#163.com),本站将尽快处理。谢谢合作!