使用 Python 与 Gmail POPerver 对话
学习知识要善于思考,思考,再思考!今天golang学习网小编就给大家带来《使用 Python 与 Gmail POPerver 对话》,以下内容主要包含等知识点,如果你正在学习或准备学习文章,就都不要错过本文啦~让我们一起来看看吧,能帮助到你就更好了!
pop 是一个相对古老的协议。第一个版本是在 1984 年指定的。至今仍在使用的版本 pop3 是在 1996 年指定的。为了尝试一下,我开始连接到 gmail pop3 服务器。
第一步是查找 pop3 设置 - 连接到哪个服务器、连接哪个端口。谷歌引导我来到这里,我在那里找到了以下信息。
pop.gmail.com
需要 ssl:是
端口:995
它提到需要 ssl。 25 年前,当我最后一次摆弄 pop 时,我还没有处理过这个问题。我担心这会让人头疼,但事实证明这并不是什么挑战;在 python 文档的帮助下,我得到了这段代码。
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 <1896.697170952@dbc.mtview.ca.us> 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 1> s: . c: quit s: +ok dewey pop3 server signing off (maildrop empty) c: <close connection>
首先发生的是服务器向客户端发送问候语。友好的。因此,我将添加代码以从服务器接收消息。
当您要求从套接字接收数据时,您必须指定缓冲区大小。文档建议使用 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 dq2mb54750689ivb\r\n'
现在我们需要开始与服务器对话。是时候创建 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() + "\r\n" s.send(msg.encode())
太好了,现在我可以尝试登录了!
tlsv1.3 b'+ok gpop ready for requests from 2601:1c0:8301:b590:f408:d66a:3029:16ad g4mb5147337iow\r\n' user grokprogramming b'+ok send pass\r\n' pass trustno1 b'-err [auth] application-specific password required: https://support.google.com/accounts/answer/185833\r\n'
好的,点击该链接后,我将进入一个可以设置应用程序特定密码的页面。我遇到的一个潜在的障碍是:据我所知,您的帐户必须启用双因素身份验证,以便您能够创建应用程序特定的密码。为什么我不在我们的 lorde 2024 年开启双因素身份验证?我不能说。我现在知道了。
有了应用程序特定的密码(注意去掉空格),我就可以登录了!然后我将发出 stat 命令,它会告诉我有多少条消息以及它们的总大小。之后,我将发出 list 命令,该命令将返回一个消息列表,其中包含每条消息的 id 和大小。
tlsv1.3 b'+ok gpop ready for requests from 2601:1c0:8301:b590:f408:d66a:3029:16ad e18mb76868856iow\r\n' user grokprogramming b'+ok send pass\r\n' pass baygdsgkmihkckrb b'+ok welcome.\r\n' stat b'+ok 263 14191565\r\n' list b'+ok 263 messages (14191565 bytes)\r\n1 2778\r\n2 2947\r\n3 6558\r\n4 9864\r\n5 35997\r\n6 45462\r\n7 45462\r\n8 63894\r\n9 11487\r\n10 74936\r\n11 74925\r\n12 11632\r\n13 32392\r\n14 74997\r\n15 51961\r\n16 15375\r\n17 46513\r\n18 21519\r\n19 15966\r\n20 27258\r\n21 28503\r\n22 35615\r\n23 86353\r\n24 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("> ") + "\r\n" s.send(msg.encode())
这是一个完整的会话,包括检索电子邮件和发送断开连接消息。
> user grokprogramming +ok send pass more? y/[n]: > pass trustno1 +ok welcome. more? y/[n]: > stat +ok 263 14191565 more? y/[n]: > list +ok 263 messages (14191565 bytes) 1 2778 2 2947 3 6558 <...> 260 41300 261 114059 262 174321 263 39206 . more? y/[n]: > 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+tzsosd_w4g5nzkks7pdk6cwq@mail.gmail.com> subject: customize gmail with colors and themes from: gmail team <mail-noreply@google.com> to: grok programming <grokprogramming@gmail.com> 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://mail.google.com/mail/#settings/themes> 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://support.google.com/mail/bin/answer.py?answer=3d6557&hl=3den&= utm_source=3dwel-eml&utm_medium=3deml&utm_campaign=3den> .. --e0cb4e385592f8025004c393f2b4 content-type: text/html; charset=iso-8859-1 more? y/[n]: y <html> <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> <img width="398" height="256" src="https://mail.google.com/mail/images/gmail_themes_2.png" alt="themes thumbnails" /> <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&hl=en&utm_source=wel-eml&utm_medium=eml&utm_campaign=en"><font color="#999"> upgrade to a fully supported browser</font></a>.</font></p> </font> </html> --e0cb4e385592f8025004c393f2b4-- . more? y/[n]: > quit +ok farewell. more? y/[n]: >
又一次巨大的成功!我能够登录 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"\r\n") def read_muli_line(s): return read_until(s, b"\r\n.\r\n") 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("> ") # empty msg will close connection while msg != "": if msg[0] == "!": msg = msg[1:] long = True else: long = False msg += "\r\n" s.send(msg.encode()) if long: print(read_muli_line(s)) else: print(read_single_line(s)) msg = input("> ") s.close()
文中关于的知识介绍,希望对你的学习有所帮助!若是受益匪浅,那就动动鼠标收藏这篇《使用 Python 与 Gmail POPerver 对话》文章吧,也可关注golang学习网公众号了解相关技术文章。

- 上一篇
- BIOS怎么更新 Win11系统更新BIOS的方法教程

- 下一篇
- Solving Word Cookies Puzzles: A Python Adventure
-
- 文章 · python教程 | 10秒前 |
- Python正则匹配Unicode转义符详解
- 460浏览 收藏
-
- 文章 · python教程 | 1分钟前 |
- Python异步编程:async/await详解教程
- 271浏览 收藏
-
- 文章 · python教程 | 5分钟前 |
- Python微服务开发:Nameko框架全解析
- 179浏览 收藏
-
- 文章 · python教程 | 19分钟前 |
- Python语音识别教程:SpeechRecognition库使用详解
- 309浏览 收藏
-
- 文章 · python教程 | 28分钟前 |
- Django入门:PythonWeb开发教程
- 303浏览 收藏
-
- 文章 · python教程 | 50分钟前 |
- Python中ord函数的作用及用法详解
- 418浏览 收藏
-
- 文章 · python教程 | 1小时前 |
- Python3文件找不到解决方法大全
- 455浏览 收藏
-
- 文章 · python教程 | 1小时前 |
- Python代码安全审计:漏洞与防护全解析
- 156浏览 收藏
-
- 文章 · python教程 | 1小时前 |
- Python处理JSON的实用技巧
- 286浏览 收藏
-
- 文章 · python教程 | 1小时前 |
- Python图像分割:UNet模型全解析
- 176浏览 收藏
-
- 文章 · python教程 | 1小时前 |
- Python中//是什么意思?整除运算符详解
- 405浏览 收藏
-
- 文章 · python教程 | 2小时前 |
- Pythonrandom模块功能与使用方法详解
- 219浏览 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 542次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 508次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 497次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 484次学习
-
- 免费AI认证证书
- 科大讯飞AI大学堂推出免费大模型工程师认证,助力您掌握AI技能,提升职场竞争力。体系化学习,实战项目,权威认证,助您成为企业级大模型应用人才。
- 31次使用
-
- 茅茅虫AIGC检测
- 茅茅虫AIGC检测,湖南茅茅虫科技有限公司倾力打造,运用NLP技术精准识别AI生成文本,提供论文、专著等学术文本的AIGC检测服务。支持多种格式,生成可视化报告,保障您的学术诚信和内容质量。
- 160次使用
-
- 赛林匹克平台(Challympics)
- 探索赛林匹克平台Challympics,一个聚焦人工智能、算力算法、量子计算等前沿技术的赛事聚合平台。连接产学研用,助力科技创新与产业升级。
- 210次使用
-
- 笔格AIPPT
- SEO 笔格AIPPT是135编辑器推出的AI智能PPT制作平台,依托DeepSeek大模型,实现智能大纲生成、一键PPT生成、AI文字优化、图像生成等功能。免费试用,提升PPT制作效率,适用于商务演示、教育培训等多种场景。
- 179次使用
-
- 稿定PPT
- 告别PPT制作难题!稿定PPT提供海量模板、AI智能生成、在线协作,助您轻松制作专业演示文稿。职场办公、教育学习、企业服务全覆盖,降本增效,释放创意!
- 169次使用
-
- Flask框架安装技巧:让你的开发更高效
- 2024-01-03 501浏览
-
- Django框架中的并发处理技巧
- 2024-01-22 501浏览
-
- 提升Python包下载速度的方法——正确配置pip的国内源
- 2024-01-17 501浏览
-
- Python与C++:哪个编程语言更适合初学者?
- 2024-03-25 501浏览
-
- 品牌建设技巧
- 2024-04-06 501浏览