-
python哄骗imap实现伪“无痕”取信
添加时间:2013-8-8 点击量:所谓无痕取信,今朝主如果指从邮箱中把信件收取后,邮箱内状况不产生任何改变。这里的状况主如果指两项目组,一项目组是邮件状况不变,即已读与未读状况不变,另一项目组是指邮箱记录的登岸IP不产生改变。本文中所说的伪“无痕”取信主如果指实现第一项目组。
一、筹办常识
1、ImaplibIMAP4.append(mailbox, flags, date_time, message):Append message to named mailbox.
IMAP4.authenticate(mechanism, authobject):Authenticate command — requires response processing.mechanism specifies which authentication mechanism is to be used - it should appear in the instance variable capabilities in the form AUTH=mechanism.
IMAP4.check():Checkpoint mailbox on server.
IMAP4.close():Close currently ed mailbox. Deleted messages are removed writable mailbox. This is the recommended command before LOGOUT.
IMAP4.copy(message_set, new_mailbox):Copy message_set messages onto end of new_mailbox.
IMAP4.create(mailbox):Create new mailbox named mailbox.
IMAP4.(mailbox):Delete old mailbox named mailbox.
IMAP4.acl(mailbox, who):Delete the ACLs (remove any rights) set for who on mailbox.
IMAP4.expunge():Permanently remove d items ed mailbox. Generates an EXPUNGE response for each d message. Returned data contains a list of EXPUNGE message numbers in order received.
IMAP4.fetch(message_set, message_parts):Fetch (parts of) messages. message_parts should be a string of message part names enclosed within parentheses, eg: (UID BODY[TEXT]). Returned data are tuples of message part envelope and data.
IMAP4.getacl(mailbox):Get the ACLs for mailbox. The method is non-standard, but is supported by the Cyrus server.
IMAP4.getannotation(mailbox, entry, attribute):Retrieve the specified ANNOTATIONs for mailbox. The method is non-standard, but is supported by the Cyrus server.
IMAP4.getquota(root):Get the quota root‘s resource usage and limits. This method is part of the IMAP4 QUOTA extension defined in rfc2087.
IMAP4.getquotaroot(mailbox):Get the list of quota roots for the named mailbox. This method is part of the IMAP4 QUOTA extension defined in rfc2087.
IMAP4.list([directory[, pattern]]):List mailbox names in directory matching pattern. directory defaults to the top-level mail folder, and pattern defaults to match anything. Returned data contains a list of LIST responses.
IMAP4.login(user, password):Identify the client using a plaintext password. The password will be quoted.
IMAP4.login_cram_md5(user, password):Force use of CRAM-MD5 authentication when identifying the client to protect the password. Will only work if the server CAPABILITY response includes the phrase AUTH=CRAM-MD5.
IMAP4.logout():Shutdown connection to server. Returns server BYE response.
IMAP4.lsub([directory[, pattern]]):List subscribed mailbox names in directory matching pattern. directory defaults to the top level directory and pattern defaults to match any mailbox. Returned data are tuples of message part envelope and data.
IMAP4.myrights(mailbox):Show my ACLs for a mailbox (i.e. the rights that I have on mailbox).
IMAP4.namespace():Returns IMAP namespaces as defined in RFC2342.
IMAP4.noop():Send NOOP to server.
IMAP4.open(host, port):Opens socket to port at host. This method is implicitly called by the IMAP4 constructor. The connection objects established by this method will be used in the read, readline, send, and shutdown methods. You may override this method.
IMAP4.partial(message_num, message_part, start, length):Fetch d part of a message. Returned data is a tuple of message part envelope and data.
IMAP4.proxyauth(user):Assume authentication as user. Allows an authorised administrator to proxy into any user’s mailbox.
IMAP4.read(size):Reads size bytes the remote server. You may override this method.
IMAP4.readline():Reads one line the remote server. You may override this method.
IMAP4.recent():Prompt server for an . Returned data is None if no new messages, else value of RECENT response.
IMAP4.rename(oldmailbox, newmailbox):Rename mailbox named oldmailbox to newmailbox.
IMAP4.response(code):Return data for response code if received, or None. Returns the given code, instead of the usual type.
IMAP4.search(charset, criterion[, ...]):Search mailbox for matching messages. charset may be None, in which case no CHARSET will be specified in the request to the server. The IMAP protocol requires that at least one criterion be specified; an exception will be raised when the server returns an error.
IMAP4.send(data):Sends data to the remote server. You may override this method.
IMAP4.setacl(mailbox, who, what):Set an ACL for mailbox. The method is non-standard, but is supported by the Cyrus server.
IMAP4.setannotation(mailbox, entry, attribute[, ...]):Set ANNOTATIONs for mailbox. The method is non-standard, but is supported by the Cyrus server.
IMAP4.setquota(root, limits):Set the quota root‘s resource limits. This method is part of the IMAP4 QUOTA extension defined in rfc2087.
IMAP4.shutdown():Close connection established in open. This method is implicitly called by IMAP4.logout(). You may override this method.
IMAP4.socket():Returns socket instance used to connect to server.
IMAP4.sort(sort_criteria, charset, search_criterion[, ...]):The sort command is a variant of search with sorting semantics for the results. Returned data contains a space separated list of matching message numbers.
IMAP4.status(mailbox, names):Request named status conditions for mailbox.
IMAP4.store(message_set, command, flag_list):Alters flag dispositions for messages in mailbox. command is specified by section 6.4.6 of RFC 2060 as being one of “FLAGS”, “+FLAGS”, or “-FLAGS”, optionally with a suffix of ”.SILENT”.
IMAP4.thread(threading_algorithm, charset, search_criterion[, ...]):The thread command is a variant of search with threading semantics for the results. Returned data contains a space separated list of thread members.
IMAP4.uid(command, arg[, ...]):ute command args with messages identified by UID, rather than message number. Returns response appropriate to command. At least one argument must be supplied; if none are provided, the server will return an error and an exception will be raised.
IMAP4.unsubscribe(mailbox):Unsubscribe old mailbox.
IMAP4.xatom(name[, arg[, ...]]):Allow simple extension commands notified by server in CAPABILITY response.
IMAP4_SSL.ssl():Returns SSLObject instance used for the secure connection with the server.
IMAP4.PROTOCOL_VERSION:The most recent supported protocol in the CAPABILITY response the server.
IMAP4.debug:Integer value to control debugging output. The initialize value is taken the module variable Debug. Values greater than three trace each command.
2、imap 号令详解
CREATE <folder>:CREATE可以创建指定名字的新邮箱。邮箱名称凡是是带路径的文件夹全名。(有些IMAP客户机应用邮件夹称呼新邮箱)
C: A003 CREATE owatagusiam/ /创建一个新目次owatagusiam/
S: A003 OK CREATE completed
C: A004 CREATE owatagusiam/blurdybloop
/ 在创建的目次owatagusiam下创建一个名为blurdybloop 的邮箱,当然可以省略第一步,直接A004 CREATE owatagusiam/blurdybloop ,默示在新的目次owatagusiam 下创建了一个名为blurdybloop 的邮箱/
S: A004 OK CREATE completedDELETE <folder>:DELETE号令删除指定名字的文件夹。文件夹名字凡是是带路径的文件夹全名,当邮箱被删除后,此中的邮件也不复存在。
C: A683 DELETE blurdybloop
S: A683 OK DELETE completed
C: A684 DELETE foo
S: A684 NO Name foo has inferior hierarchical names
C: A685 DELETE foo/bar
S: A685 OK DELETE CompletedRENAME <old folder><new folder>:RENAME号令可以批改文件夹的名称,它应用两个参数:当前邮箱名和新邮箱名,两个参数的定名合适标准路径定名规矩。
C: A683 RENAME blurdybloop sarasoop
S: A683 OK RENAME completed
C: A684 RENAME stuff/junk newbox /把stuff目次(文件夹)下的邮箱junk改名为newbox/
S: A684 OK RENAME CompletedLIST <BASE><template>:LIST号令用于列出邮箱中已有的文件夹,有点像操纵体系的列目次号令,有两个参数,邮箱路径参数BASE:默示用户登岸目次;第二个参数template:默示显示的邮箱名。这个号令可以包含肇端的路径地位和须要列出的文件夹所合适的特点,可以应用通配符。
C: A101 LIST
S: LIST (\No) /
S: A101 OK LIST Completed
C: A102 LIST #news.comp.mail.misc
S: LIST (\No) . #news.
S: A102 OK LIST Completed
C: A103 LIST /usr/staff/jones
S: LIST (\No) / /
S: A103 OK LIST Completed
C: A202 LIST ~/Mail/ %
S: LIST (\No) / ~/Mail/foo
S: LIST () / ~/Mail/meetings
S: A202 OK LIST completedAPPEND <folder><attributes><date/time><size><mail data>:APPEND号令容许Client上载一个邮件到指定的Folder(文件夹/邮箱)中。号令中包含了新邮件的属性、日期/时候、大小,随后是邮件数据。
C: A003 APPEND saved-messages (\Seen) {310}
C: Date: Mon, 7 Feb 1994 21:52:25 -0800 (PST)
C: From: Fred Foobar <foobar@Blurdybloop.COM>
C: Subject: afternoon meeting
C: To: mooch@owatagu.siam.edu
C: Message-Id: <B27397-0100000@Blurdybloop.COM>
C: MIME-Version: 1.0
C: Content-Type: TEXT/PLAIN; CHARSET=US-ASCII
C:
C: Hello Joe, do you think we can meet at 3:30 tomorrow?
C:
S: A003 OK APPEND completedSELECT <folder>:SELECT号令让Client选定某个邮箱(Folder),默示即将对该邮箱(Folder)内的邮件作操纵。邮箱标记的当前状况也返回给了用户,同时返回的还有一些关于邮件和邮箱的附加信息。
C: A142 SELECT INBOX
S: 172 EXISTS
S: 1 RECENT
S: OK [UNSEEN 12] Message 12 is first unseen
S: OK [UIDVALIDITY 3857529045] UIDs valid
S: FLAGS (\Answered \Flagged \Deleted \Seen \Draft)
S: OK [PERMANENTFLAGS (\Deleted \Seen \)] Limited
S: A142 OK [READ-WRITE] SELECT completedFETCH <mail id><datanames>:FETCH 号令用于读取邮件的文本信息,且仅用于显示的目标。包含两个参数,messageset:默示读取的邮件号列表,IAMP办事器邮箱中的每个邮件都有 一个独一的ID标识,(邮件号列表参数可所以一个邮件号,也可所以由逗号分隔的多个邮件号,或者由冒号间隔的一个局限),IMAP办事器返回邮件号列表中 全部邮件的指定命据项内容。
数据名参数断定可以或许被自力返回的邮件的一项目组,下面我们看看各参数返回的邮件信息:
ALL:只返回遵守必然格局的邮件择要,包含邮件标记、RFC822.SIZE、自身的时候和信封信息。IMAP客户性可以或许将标准邮件解析成这些信息并显示出来。
BODY:只返回邮件体文本格局和大小的择要信息。IMAP客户机可以辨认这些细腻,并向用户显示具体的关于邮件的信息。其实是一些非扩大的BODYSTRUCTURE的信息。
FAST:只返回邮件的一些择要,包含邮件标记、RFC822.SIZE、和自身的时候。
FULL:同样的还是一些择要信息,包含邮件标记、RFC822.SIZE、自身的时候和BODYSTRUCTURE的信息。
BODYSTRUCTUR: 是邮件的[MIME-IMB]的体布局。这是办事器经由过程解析[RFC-2822]头中的[MIME-IMB]各字段和[MIME-IMB]头信息得出来 的。包含的内容有:邮件正文的类型、字符集、编码体式格式等和各附件的类型、字符集、编码体式格式、文件名称等等。
ENVELOPE:信息的信封布局。是办事器经由过程解析[RFC-2822]头中的[MIME-IMB]各字段得出来的,默认各字段都是须要的。首要包含:自身的时候、附件数、收件人、发件人等。
FLAGS:此邮件的标记。
INTERNALDATE:自身的时候。
RFC822.SIZE:邮件的[RFC-2822]大小
RFC822.HEADER:在功能上等同于BODY.PEEK[HEADER],
RFC822:功能上等同于BODY[]。
RFC822.TEXT:功能上等同于BODY[TEXT]
UID:返回邮件的UID号,UID号是独一标识邮件的一个号码。
BODY[section] <<partial>>:返回邮件的中的某一指定项目组,返回的项目组用section来默示,section项目组包含的信息凡是是 代表某一项目组的一个数字或者是下面的某一个项目组:HEADER, HEADER.FIELDS, HEADER.FIELDS.NOT, MIME, and TEXT。若是section项目组是空的话,那就代表返回全部的信息,包含头信息。
BODY[HEADER]返回完全的文件头信息。
BODY[HEADER.FIELDS ()]:在小括号里面可以指定返回的特定字段。
BODY[HEADER.FIELDS.NOT ()]:在小括号里面可以指定不须要返回的特定字段。
BODY[MIME]:返回邮件的[MIME-IMB]的头信息,在正常景象下跟BODY[HEADER]没有差别。
BODY[TEXT]:返回全部邮件体,这里的邮件体并不包含邮件头。STORE <mail id><new attributes>:STORE 号令用于批改指定邮件的属性,包含给邮件打上已读标识表记标帜、删除标识表记标帜,等等。STORE号令当前只有两个数据项类型可用,FLAGS:默示邮件的一组标记; FLAGS.SLIENT,默示一组邮件的标记,经由过程在两种数据项前加上加号或者减号可以进一步改变它们的履行景象,加号默示数据项的值添加到邮件中,减 号默示将数据项的值从邮件中删除。
C: A003 STORE 2:4 +FLAGS (\Deleted) /冒号默示间隔的一个局限:给从2到4的邮件设置Deleted属性/
S: 2 FETCH FLAGS (\Deleted \Seen)
S: 3 FETCH FLAGS (\Deleted)
S: 4 FETCH FLAGS (\Deleted \Flagged \Seen)
S: A003 OK STORE completed
同时改号令还可以用于把邮件标识表记标帜为未读等等。在web上可以对邮件进行的操纵在imap中都可以实现。CLOSE:CLOSE号令默示Client停止对当前Folder(文件夹/邮箱)的接见,封闭邮箱该邮箱中所有标记为、DELETED的邮件就被从物理上删除。CLOSE没有号令参数。随后可以SELECT另一Folder。
C: A341 CLOSE
S: A341 OK CLOSE completed
EXPUNGE:EXPUNGE号令在不封闭邮箱的景象下删除所有的标记为、DELETED的邮件。EXPUNGE删除的邮件将不成以恢复。
C: A202 EXPUNGE
S: 3 EXPUNGE
S: 3 EXPUNGE
S: 5 EXPUNGE
S: 8 EXPUNGE
S: A202 OK EXPUNGE completedLOGOUT:LOGOUT号令停止本次IMAP会话。
C: A023 LOGOUT
S: BYE IMAP4rev1 Server logging out
S: A023 OK LOGOUT completed
(Server and client then close the connection)EXAMINE <mailbox>:
EXAMINE号令以只读体式格式打开邮箱,参数是须要打开的邮箱的名字,应用EXAMINE号令打开的邮箱不容许对邮件进行批改,是以不克不及增长或删除邮件的标记。SUBSCRIBE <mailbox>:SUBSCRIBE号令用来在客户机的活动邮箱列表中增长一个邮箱,该号令只有一个参数,添加的邮箱名。
C: A114 SUBSCRIBE new/anotherbox
S: A114 OK SUBSCRIBE completedUNSUBSCRIBE <mailbox>:UNSUBSCRIBE号令用来从活动列表中去掉一个邮箱,一个参数:去掉的邮箱名。
C: A115 UNSUBSCRIBE new/anotherbox
S: A115 OK SUBSCRIBE completedLSUB <folder><mailbox>:LSUB号令批改了LIST号令,LIST返回用户¥HOME目次下所有的文件,但LSUB号令只显示那些应用SUBSCRIBE号令设置为活动邮箱的文件。两个参数:邮箱路径和邮箱名。
C: A116 LSUB “”
S: LSUB () “/” stuff/junk
S: LSUB () “/” neebox
S: LSUB () “/” new/anotherbox
S: A116 OK LSUB completedSTATUS <mailbox>(<parameter1> < parameter2> ……<parameter5>):STATUS号令查询邮箱的当前状况。第一个参数是须要查询的邮箱名,第二个参数是客户机须要查询的项目列表(要查询显示的信息),当在圆括号中。STATUS可以在不应用SELECT号令(打开邮箱)或者EXAMINE(以只读体式格式打开邮箱)前提下获取邮箱的信息。
STATUS号令可以获得的数据项
项 目 说 明
MESSAGE 邮箱中的邮件总数
RECENT 邮箱中标记为\RECENT的邮件数
UIDNEXT 可以分派给新邮件的下一个UID
UIDVALIDITY 邮箱的UID有效性标记
UNSEEN 邮箱中没有被标记为\UNSEEN的邮件数
C: A117 STATUS inbox (message recent unseen)
S: STATUS inbox (MESSAGE 1 RECENT 0 UNSENN 0)
S: A117 OK STATUS completed
C: A118 STATUS newbox (message recent unseen)
S: STATUS inbox (MESSAGE 1 RECENT 0 UNSENN 2)
S: A118 OK STATUS completedCHECK :CHECK号令用来在邮箱设置一个搜检核。没有参数。就是IMAP中的sync号令。任何未完成的操纵,例如从办事器内存向硬盘写数据,都将会被做完以对峙邮箱的一致性状况。该号令确保乃村中的磁盘缓冲数据都被写到了磁盘上。
SEARCH [CHARSET specification] (search criteria):命 令可以按照搜刮前提在处于活动状况的邮箱中搜刮邮件,然后显示匹配的邮件编号。字符集标记参数[CHARSET specification]由CHARSET和注册的字符集标记符构成,缺省的标记符是US-ASCⅡ,所以该参数长省略。search criteria:查询前提参数,明白查询的关键字和值。查询关键字有几十种。
C: A119 SEARCH header subject another
S: SEARCH 1 2
S: A119 OK SEARCH completed
C: A120 SEARCH header subject another
S: SEARCH 2
S: A120 OK SEARCH completed
C: A121 SEARCH UNSEEN
S: SEARCH 1 2
S: A120 OK SEARCH completed
以上每个例子都在邮件头的Subject:字段中查询一个不合的单词。办事器返回前提的邮件号列表,若是没有匹配邮件则返回不带UID的SEARCH单词。COPY <mail id><mailboxname>:COPY号令可以把邮件从一个邮箱复制到另一个邮箱,两个参数:mail id是从活动邮箱中复制的邮件的标号,mailboxname是邮件被复制到的邮箱。
IAMP没有定义移动邮件的号令,移动操纵相当于先把邮件复制到新邮箱中,然后对源邮箱中的邮件设置\DELETED标记。下一次履行搜检核过后,新邮箱中的邮件被删除,新邮件就被显示出来。UID:UID 号令和FETCH、COPY、STORE号令或者SEARCH号令一路应用,它容许这些号令应用邮件的UID号而不是在邮箱中的次序号。UID号是独一标 识邮件体系中邮件的32位。凡是这些号令都应用次序号来标识邮箱中的邮件,应用UID可以使IMAP客户机记住不合IMAP会话中的邮件。
CAPABILITY:CAPABILITY号令恳求返回IMAP办事器支撑的功能列表,办事器收到客户机发送的CAPABILITY号令后将返回该办事器所支撑的功能。无参数。
C: A122 CAPABILITY
S:A122 CAPABILITY IMAP4 IMAP4REVl NAMESPACE IDLE SCAN SORT MAILBOX
--REFERRALS [ic:ccc] LOGIN-REFERRALS AUTH=LOGIN THREAD=
ORDERDSUBJECT
S: A122 OK CAPABILITY completedNOOP:NOOP号令什么也不做,用来向办事器发送主动号令,防止因长时候处于不活动状况而导致连接中断,办事器对该号令的响应始终为必然。无参数。
LOGOUT:LOGOUT号令使当前登岸用户退出登岸并封闭所有打开的邮箱,任何做了\DELETED标记的邮件都将在这个时辰被删除。
二、以yahoo邮箱为例进行伪无痕收信
要做到邮件状况不产生变更,实现上关键在fetch的参数上。若是我们仅是读取邮件头BODY.PEEK[HEADER],那么邮件的状况是不产生变更的。若是我们要读取全部邮件内容,那么必须应用BODY.PEEK[],如许才干包管不产生变更。如下面的例子:#-- coding:UTF-8 --
#@小五义 http://www.cnblogs.com/xiaowuyi
import imaplib, string, email
import os
M = imaplib.IMAP4_SSL(imap.mail.yahoo.com,993)
t=0
try:
try:
M.login(XXXX@yahoo.com,YYYY)####YYYY为暗码
except Exception,e:
#print wrong!
print login error: %s % e
M.close()
M.(INBOX,False)
result, message = M.()
typ, data = M.search(None, ALL)
for num in string.split(data[0]):
try:
typ, data = M.fetch(num, (UID BODY.PEEK[]))
msg = email.message__string(data[0][1])
t=t+1
filename=str(t)+.eml
f=open(filename,wb)
f.write(str(msg))
f.close
except Exception,e:
print got msg error: %s % e
print OK!
M.logout()
except Exception, e:
print imap error: %s % e
M.close()前面说的有些抽象,typ, data = M.fetch(num, (UID BODY.PEEK[]))在这一句中,可以偿试着将UID BODY.PEEK[HEADER]、UID BODY[]、RFC822、RFC822.HEADER等进行收信斗劲,将会加倍直观。这里就不再一一申明。
无论对感情还是对生活,“只要甜不要苦”都是任性而孩子气的,因为我们也不完美,我们也会伤害人。正因为我们都不完美,也因为生活从不是事事如意,所以对这些“瑕疵”的收纳才让我们对生活、对他人的爱变得日益真实而具体。—— 汪冰《世界再亏欠你,也要敢于拥抱幸福》