您的位置 首页 编程知识

使用 Python 与 Gmail POPerver 对话

pop 是一个相对古老的协议。第一个版本是在 1984 年指定的。至今仍在使用的版本 pop3 是在 1996…

使用 Python 与 Gmail POPerver 对话

pop 是一个相对古老的协议。第一个版本是在 1984 年指定的。至今仍在使用的版本 pop3 是在 1996 年指定的。为了尝试一下,我开始连接到 gmail pop3 服务器。

第一步是查找 pop3 设置 – 连接到哪个服务器、连接哪个端口。谷歌引导我来到这里,我在那里找到了以下信息。

pop.gmail.com 需要 ssl:是 端口:995

它提到需要 ssl。 25 年前,当我最后一次摆弄 pop 时,我还没有处理过这个问题。我担心这会让人头疼,但事实证明这并不是什么挑战;在 文档的帮助下,我得到了这段代码。

import socket import ssl  hostname = 'pop.gmail.com' context = ssl.create_default_context()  with socket.create_connection((hostname, 995)) as sock:     with context.wrap_socket(sock, server_hostname=hostname) as s:         print(s.version()) 
登录后复制

它会连接,并告诉我正在使用的 ssl 版本……或者其他什么。伟大的成功!是时候开始与服务器对话了。

借用 pop3 的官方 rfc,这是客户端和服务器之间的 pop3 对话示例/

c: <open connection> s:    +ok pop3 server ready  c:    user mrose s:    +ok mrose is a real hoopy frood c:    pass secret s:    +ok mrose's maildrop has 2 messages (320 octets) c:    stat s:    +ok 2 320 c:    list s:    +ok 2 messages (320 octets) s:    1 120 s:    2 200 s:    . c:    retr 1 s:    +ok 120 octets s:    <the pop3 server sends message> s:    . c:    quit s:    +ok dewey pop3 server signing off (maildrop empty) c:  <close connection></close></the></open>
登录后复制

首先发生的是服务器向客户端发送问候语。友好的。因此,我将添加代码以从服务器接收消息。

立即学习“”;

当您要求从套接字接收数据时,您必须指定缓冲区大小。文档建议使用 2 的幂,例如 4096。来自服务器的许多响应将同时通过。有些不会;有些则不会。有时来自服务器的消息会在服务器读取时被破坏,并且即使还有更多消息,缓冲区也可能不会填满。

对于 pop3,判断消息是否已传入的方法完全取决于传入的消息。大多数情况下,服务器发送单行文本。 (正如我们稍后将再次看到的,它们在每行末尾都有一个回车符和换行符。)某些可能具有更长响应的消息使用另一种方式来显示它们已完成:单行上的句点就其本身而言。

import socket import ssl  hostname = 'pop.gmail.com' context = ssl.create_default_context()  with socket.create_connection((hostname, 995)) as sock:     with context.wrap_socket(sock, server_hostname=hostname) as s:         print(s.version())         data = s.read(4096)         print(data) 
登录后复制

再次运行,我们收到了问候。又一次巨大的成功!请注意,该行以“rn”结尾——回车符和换行符。

您必须将缓冲区大小传递给读取方法。然后它将有一个缓冲区,其大小可用于从服务器读取数据——但不能保证一次有多少数据进入缓冲区。这意味着协议需要某种方式来指定消息何时完成。有多种可能的策略。 pop 使用两个:对于所有消息,行都以 rn 结尾。对于短(一行)消息,这就是所需要的。对于多行响应,一行上的句点表示消息已完成。

tlsv1.3 b'+ok gpop ready for requests from 2601:1c0:8301:b590:f408:d66a:3029:16ad dq2mb54750689ivbrn' 
登录后复制

现在我们需要开始与服务器对话。是时候创建 i/o(或 o/i)循环了;获取一些用户输入并将其发送到服务器。哎呀!我无法直接发送字符串;这给了我一个类型错误。我需要将消息转换为字节。 stringencode() 方法可以做到这一点(默认编码 utf-8 工作正常)。

只是,当我运行它时——哎呀又来了!当我的消息发送到服务器时没有任何反应。因为我忘记了来自客户端的消息也需要以 rn 结尾。另一个微小的调整给了我们:

import socket import ssl  hostname = 'pop.gmail.com' context = ssl.create_default_context()  with socket.create_connection((hostname, 995)) as sock:     with context.wrap_socket(sock, server_hostname=hostname) as s:         print(s.version())         while true:             data = s.read(4096)             print(data)             msg = input() + "rn"             s.send(msg.encode()) 
登录后复制

太好了,现在我可以尝试登录了!

tlsv1.3 b'+ok gpop ready for requests from 2601:1c0:8301:b590:f408:d66a:3029:16ad g4mb5147337iowrn' user grokprogramming b'+ok send passrn' pass trustno1 b'-err [auth] application-specific password required: https://support.google.com/accounts/answer/185833rn' 
登录后复制

好的,点击该链接后,我将进入一个可以设置应用程序特定密码的页面。我遇到的一个潜在的障碍是:据我所知,您的帐户必须启用双因素身份验证,以便您能够创建应用程序特定的密码。为什么我在我们的 lorde 2024 年开启双因素身份验证?我不能说。我现在知道了。

有了应用程序特定的密码(注意去掉空格),我就可以登录了!然后我将发出 stat 命令,它会告诉我有多少条消息以及它们的总大小。之后,我将发出 list 命令,该命令将返回一个消息列表,其中包含每条消息的 id 和大小。

tlsv1.3 b'+ok gpop ready for requests from 2601:1c0:8301:b590:f408:d66a:3029:16ad e18mb76868856iowrn' user grokprogramming b'+ok send passrn' pass baygdsgkmihkckrb b'+ok welcome.rn' stat b'+ok 263 14191565rn' list b'+ok 263 messages (14191565 bytes)rn1 2778rn2 2947rn3 6558rn4 9864rn5 35997rn6 45462rn7 45462rn8 63894rn9 11487rn10 74936rn11 74925rn12 11632rn13 32392rn14 74997rn15 51961rn16 15375rn17 46513rn18 21519rn19 15966rn20 27258rn21 28503rn22 35615rn23 86353rn24 280'  
登录后复制

我在代码中遇到了一个错误。 list 的响应跨越多行,在这种情况下,将需要多次缓冲区读取。整条消息将以单独一行的句点结束。在这里,我已经收到了一个缓冲区的消息,现在我必须按回车键并向服务器发送一条空白消息,以便代码前进到循环的下一次迭代并再次从缓冲区读取。

我将调整代码,以便用户始终可以选择是否再次从缓冲区读取。我还将最终解码从服务器传入的字节,以便文本呈现得更好。

import socket import ssl  hostname = 'pop.gmail.com' context = ssl.create_default_context()  with socket.create_connection((hostname, 995)) as sock:     with context.wrap_socket(sock, server_hostname=hostname) as s:         print(s.version())         while true:             data = s.read(4096)             print(data.decode())             while input("more? y/[n]: ") == "y":                 data = s.read(4096)                 print(data.decode())             msg = input("&gt; ") + "rn"             s.send(msg.encode()) 
登录后复制

这是一个完整的会话,包括检索电子邮件和发送断开连接消息。

&gt; user grokprogramming +ok send pass  more? y/[n]:  &gt; pass trustno1 +ok welcome.  more? y/[n]:  &gt; stat +ok 263 14191565  more? y/[n]:  &gt; list +ok 263 messages (14191565 bytes) 1 2778 2 2947 3 6558 <...> 260 41300 261 114059 262 174321 263 39206 .  more? y/[n]:  &gt; retr 1 +ok message follows mime-version: 1.0 received: by 10.76.81.230; thu, 28 jun 2012 20:21:50 -0700 (pdt) date: thu, 28 jun 2012 20:21:50 -0700 message-id: <cadbp03twfokctoak_0p7vv2gb> subject: customize gmail with colors and themes from: gmail team <mail-noreply> to: grok programming <grokprogramming> content-type: multipart/alternative; boundary=e0cb4e385592f8025004c393f2b4  --e0cb4e385592f8025004c393f2b4 content-type: text/plain; charset=iso-8859-1 content-transfer-encoding: quoted-printable  to spice up your inbox with colors and themes, check out the themes tab under settings.        customize gmail =bb <https:>  enjoy!  - the gmail team [image: themes thumbnails]  please note that themes are not available if you're using internet explorer 6.0. to take advantage of the latest gmail features, please upgrade to a fully supported browser<http: utm_source="3dwel-eml&amp;utm_medium=3deml&amp;utm_campaign=3den"> ..  --e0cb4e385592f8025004c393f2b4 content-type: text/html; charset=iso-8859-1  more? y/[n]: y   <font face="arial, helvetica, sans-serif"> <p>to spice up your inbox with colors and themes, check out the themes tab under settings.</p>  <table cellpadding="0" cellspacing="0"> <col style="width: 1px;"> <col> <col style="width: 1px;"> <tr> <td></td>     <td height="1px" style="background-color: #ddd"></td>     <td></td>   </tr> <tr> <td style="background-color: #ddd"></td>     <td background="https://mail.google.com/mail/images/welcome-button-background.png" style="background-color: #ddd; background-repeat: repeat-x;             padding: 10px; font-size: larger">           <a href="https://mail.google.com/mail/#settings/themes" style="font-weight: bold; color: #000; text-decoration: none;             display: block;">       customize gmail »</a>     </td>     <td style="ba more? y/[n]: y ckground-color: #ddd"></td>   </tr> <tr> <td></td>     <td height="1px" style="background-color: #ddd"></td>     <td></td>   </tr> </table> <p>enjoy!</p>  <p>- the gmail team</p>  @@##@@  <p><font size="-2" color="#999">please note that themes are not available if you're using internet explorer 6.0. to take advantage of the latest gmail features, please <a href="http://support.google.com/mail/bin/answer.py?answer=6557&amp;hl=en&amp;utm_source=wel-eml&amp;utm_medium=eml&amp;utm_campaign=en"><font color="#999"> upgrade to a fully supported browser</font></a>.</font></p>  </font>   --e0cb4e385592f8025004c393f2b4-- .  more? y/[n]:  &gt; quit +ok farewell.  more? y/[n]:  &gt;  </http:></https:></grokprogramming></mail-noreply></cadbp03twfokctoak_0p7vv2gb></...>
登录后复制

又一次巨大的成功!我能够登录 pop3 服务器并检索邮件。当前状态下的脚本非常灵活,但需要用户做大量工作。我将进行一些最后的调整,以使与 pop3 服务器的交互更加容易:如果用户使用“!”开始向服务器发送消息。它会被删除,但脚本将从服务器读取数据,直到它自己到达一行上的句点 – 换句话说,对于具有长响应的命令。不 ”!”脚本将在一行中读取,寻找 rn 字符。

import socket import ssl  hostname = 'pop.gmail.com' context = ssl.create_default_context()  def read_until(s, eom):     # read into the buffer at least once     data = s.read(4096)     # continue reading until end of message     while data[-len(eom):] != eom:         data += s.read(4096)     # return incoming bytes decoded to a string     return data.decode()  def read_single_line(s):     return read_until(s, b"rn")  def read_muli_line(s):     return read_until(s, b"rn.rn")  with socket.create_connection((hostname, 995)) as sock:     with context.wrap_socket(sock, server_hostname=hostname) as s:         print(s.version())         print(read_single_line(s))         msg = input("&gt; ")         # empty msg will close connection         while msg != "":             if msg[0] == "!":                 msg = msg[1:]                 long = True             else:                 long = False             msg += "rn"             s.send(msg.encode())             if long:                 print(read_muli_line(s))             else:                 print(read_single_line(s))             msg = input("&gt; ")         s.close() 
登录后复制

使用 Python 与 Gmail POPerver 对话

以上就是使用 Python 与 Gmail POPerver 对话的详细内容,更多请关注php中文网其它相关文章!

本文来自网络,不代表四平甲倪网络网站制作专家立场,转载请注明出处:http://www.elephantgpt.cn/1584.html

作者: nijia

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

联系我们

联系我们

18844404989

在线咨询: QQ交谈

邮箱: 641522856@qq.com

工作时间:周一至周五,9:00-17:30,节假日休息

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

关注微博
返回顶部