用imaplib读取电子邮件 - “获得超过10000字节”错误 [英] Reading emails with imaplib - "Got more than 10000 bytes" error

查看:130
本文介绍了用imaplib读取电子邮件 - “获得超过10000字节”错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用imaplib连接到我的Gmail账户:

 导入imaplib 
mail = imaplib。 IMAP4_SSH('imap.gmail.com')
mail.login(邮件地址','mypassword')
mail.select(收件箱)
# OK',[b'12009'])

然而,这一切似乎都很好: p>

  mail.search(无,ALL)
#返回错误:command:SEARCH =>获得超过10000字节
mail.logout()
#返回('NO',
#[< class'imaplib.IMAP4.error'> ;:命令:LOGOUT => ;得到超过10000字节])

我试图访问的帐户大约有9,000封电子邮件在收件箱中。我试着用另一个账户少于1,000的代码进行了上述尝试,代码正常。



第一个电子邮件帐户与邮件数量有关吗?是否有一些默认设置可以实现一些尺寸限制?



如何解决错误并阅读我的电子邮件?

解决方案

< blockquote>

第一个电子邮件帐户的问题与邮件数量有关吗?

不是直接,但是,很多。问题在于,您试图一次下载9000条消息的全部列表。

发送可笑的长行是一种有用的DoS攻击,对于以C而不是Python实现的程序,针对许多网络客户端和服务器的缓冲区溢出攻击。它也可能很慢,并扼杀网络。但请注意,RFC是在1999年最后更新的, imaplib 是在1997年编写的,所以自那以后荒谬的限制可能已经发生了变化。


$ b

根据 > RFC 2683 ,是不要试图做到这一点。 (详见第3.2.1.5节)





是否有一些默认设置实现了一些大小限制?


是的。它没有在文档中列出,但由于RFC建议限制为8000个字节,并且允许使用10000个字节,我想这是合理的。


如何避免错误并阅读我的电子邮件?

然而,只要gmail对搜索没有任何问题,你就很高兴要求计算机和网络连接比90年代后期标准稍好一点,你可能会避开解决问题。



幸运的是,像许多stdlib中的模块 imaplib 被编写成可用作示例代码的模块。你总是可以知道这是因为文档链接到来源位于顶部。



所以,如果你看一下,你会发现,离顶部不远:

 #阅读任意长度的线。 RFC 3501和2060(IMAP 4rev1)
#不指定行长度。但RFC 2683建议将客户端
#命令行限制为1000个八位字节,并将服务器命令行限制为8000个八位字节。
#我们选择了10000作为额外的保证金,因为据推测
#也是UW和熊猫IMAP所做的。
_MAXLINE = 10000

所以,如果你想覆盖这个, (将 imaplib.py 保存为 myimaplib.py ,然后使用它),或者您可以在运行时对它进行monkeatch:

 导入imaplib 
imaplib._MAXLINE = 40000

当然,你必须选择一个你认为更能反映2014年荒谬边缘的数字。


I'm trying to connect to my gmail account with imaplib:

import imaplib
mail = imaplib.IMAP4_SSH('imap.gmail.com')
mail.login('myemail@gmail.com', 'mypassword')
mail.select("inbox")
# returns ('OK', [b'12009'])

This all seems to work nicely, however:

mail.search(None, "ALL")
# returns error: command: SEARCH => got more than 10000 bytes
mail.logout()
# returns ('NO',
# ["<class 'imaplib.IMAP4.error'>: command: LOGOUT => got more than 10000 bytes"])

The account I'm trying to access has about 9,000 emails in the the inbox. I tried the above with another account which has less than 1,000 and the code works fine.

Is the issue with the first email account related to the number of mails in it? Is there some default setting that implements some size limit?

How can I get around the error and read my emails?

解决方案

Is the issue with the first email account related to the number of mails in it?

Not directly, but yeah, pretty much. The issue is with the fact that you're trying to download the whole list of 9000 messages at once.

Sending ridiculously long lines has been a useful DoS attack and, for programs implemented in C rather than Python, buffer overflow attack against many network clients and servers. It can also be very slow, and choke the network. But notice that the RFC was last updated in 1999, and imaplib was written in 1997, so the limits of "ridiculous" may have changed since then.

The right way to solve this, according to RFC 2683, is to not try to do that. (See especially section 3.2.1.5.)


Is there some default setting that implements some size limit?

Yes. It's not listed in the docs, but since the RFC recommends a limit of 8000 bytes, and it's allowing 10000, I guess that's reasonable.


How can I get around the error and read my emails?

Again, what you should do is break this up into smaller reads.

But as long gmail has no problem with a search this big, and you're happy to require a computer and network connection a little better than late-90s-standard, you can probably get away with getting around the problem instead.

Fortunately, like many of the modules in the stdlib, imaplib is written as much to be useful sample code as to be used as a module. You can always tell this is the case because the documentation links to the source right at the top.

So, if you take a look, you'll see, not far from the top:

# reading arbitrary length lines. RFC 3501 and 2060 (IMAP 4rev1)
# don't specify a line length. RFC 2683 however suggests limiting client
# command lines to 1000 octets and server command lines to 8000 octets.
# We have selected 10000 for some extra margin and since that is supposedly
# also what UW and Panda IMAP does.
_MAXLINE = 10000

So, if you want to override this, you could fork the module (save imaplib.py as myimaplib.py and use that instead), or you could just monkeypatch it at runtime:

import imaplib
imaplib._MAXLINE = 40000

Of course you'll have to pick a number that you think better reflects the edge of ridiculousness in 2014.

这篇关于用imaplib读取电子邮件 - “获得超过10000字节”错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆