发邮件自动回复本机IP——python版本

【转载自】:http://blog.csdn.net/wds2006sdo/article/details/51210896

这几天在写爬虫,有时候在宿舍需要远程登陆到实验室的主机上对代码进行一些微调,但是因为动态ip经常不知道实验室主机的IP是多少,因此产生了写这个小工具的想法。 
工作原理描述: 首先需要一个专用的邮箱,该工具每隔2分钟查询有没有新邮件。当用户向该邮箱发送邮件查询ip时,该工具接收邮件并获取本机IP,并将该IP发送给用户。 
思路很清晰,需要解决的问题很明确 1) 接收邮件 2) 发送邮件 3) 获取IP 4) 设置开机启动

让我们开始Geek之旅 o(*≧▽≦)ツ

  • 备用邮箱设置
  • 接收邮件
  • 发送邮件
  • 获取本机IP
  • 代码
  • 设置开机启动项
  • 运行结果

备用邮箱设置

我们需要准备一个备用的邮箱,我选择的是126的邮箱,邮箱要开启POP3与IMAP服务,如下图

接收邮件

接收邮件需要 imaplib 包,目前来说我们只需要解析出邮件发送方、发送日期以及发送标题即可,不需要解析邮件内容,解析代码如下所示:

<code class="language-python hljs  has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">    <span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">def</span> <span class="hljs-title" style="box-sizing: border-box;">get_mail</span><span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">(self)</span>:</span>
        imapServer = imaplib.IMAP4(self.receive_mail_host, self.port)
        imapServer.login(self.account, self.password)

        <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># 获取最近的一份邮件</span>
        resp, items = imapServer.select(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'INBOX'</span>)
        resp, mailData = imapServer.fetch(items[<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>], <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"(RFC822)"</span>)
        msg = email.message_from_string(mailData[<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>][<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>])

        <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># 获取邮件基本信息</span>
        ls = msg[<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"From"</span>].split(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">' '</span>)
        address = ls[<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>][<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>:-<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>]
        subject = email.Header.decode_header(msg[<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">"Subject"</span>])
        sub = self.my_unicode(subject[<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>][<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>], subject[<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">0</span>][<span class="hljs-number" style="color: rgb(0, 102, 102); box-sizing: border-box;">1</span>])

        imapServer.close()

        <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># Message —— 邮件信息类</span>
        mail_info = Message(address,sub.encode(<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'utf-8'</span>),msg[<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'Date'</span>],<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">''</span>)
        <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> mail_info</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li><li style="box-sizing: border-box; padding: 0px 5px;">14</li><li style="box-sizing: border-box; padding: 0px 5px;">15</li><li style="box-sizing: border-box; padding: 0px 5px;">16</li><li style="box-sizing: border-box; padding: 0px 5px;">17</li><li style="box-sizing: border-box; padding: 0px 5px;">18</li><li style="box-sizing: border-box; padding: 0px 5px;">19</li><li style="box-sizing: border-box; padding: 0px 5px;">20</li></ul>

发送邮件

发送邮件需要 smtplib 与 email 这两个包, send_info是邮件信息类Message的一个实例,里面包括接收方的邮件地址,邮件的标题subject,邮件正文detail_msg。代码如下所示:

<code class="language-python hljs  has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">    <span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">def</span> <span class="hljs-title" style="box-sizing: border-box;">send_mail</span><span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">(self,send_info, type = <span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'text'</span>)</span>:</span>
        <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># 构建邮件</span>
        msg = MIMEText(send_info.detail_msg,<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'html'</span>,<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'utf-8'</span>)
        msg[<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'Subject'</span>] = send_info.subject
        msg[<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'From'</span>] = self.send_email_from
        msg[<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">'To'</span>] = send_info.address

        <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;"># 发送邮件</span>
        smtp = smtplib.SMTP()
        smtp.connect(self.send_mail_host)
        smtp.login(self.account, self.password)
        smtp.sendmail(self.account, [send_info.address], msg.as_string())
        smtp.quit()</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li><li style="box-sizing: border-box; padding: 0px 5px;">8</li><li style="box-sizing: border-box; padding: 0px 5px;">9</li><li style="box-sizing: border-box; padding: 0px 5px;">10</li><li style="box-sizing: border-box; padding: 0px 5px;">11</li><li style="box-sizing: border-box; padding: 0px 5px;">12</li><li style="box-sizing: border-box; padding: 0px 5px;">13</li></ul>

获取本机IP

本机IP可以通过 socket包获得。代码如下所示:

<code class="language-python hljs  has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: "Source Code Pro", monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-function" style="box-sizing: border-box;"><span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">def</span> <span class="hljs-title" style="box-sizing: border-box;">get_ip</span><span class="hljs-params" style="color: rgb(102, 0, 102); box-sizing: border-box;">()</span>:</span>
    <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">#获取本机电脑名</span>
    myname = socket.getfqdn(socket.gethostname())

    <span class="hljs-comment" style="color: rgb(136, 0, 0); box-sizing: border-box;">#获取本机ip</span>
    myaddr = socket.gethostbyname(myname)
    <span class="hljs-keyword" style="color: rgb(0, 0, 136); box-sizing: border-box;">return</span> myaddr</code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li><li style="box-sizing: border-box; padding: 0px 5px;">2</li><li style="box-sizing: border-box; padding: 0px 5px;">3</li><li style="box-sizing: border-box; padding: 0px 5px;">4</li><li style="box-sizing: border-box; padding: 0px 5px;">5</li><li style="box-sizing: border-box; padding: 0px 5px;">6</li><li style="box-sizing: border-box; padding: 0px 5px;">7</li></ul>

代码

代码我已经放到了CSDN的资源中,大家可以下载下来共同学习! 
http://download.csdn.net/detail/wds2006sdo/9503976

设置开机启动项

我的系统为win10,开机启动项非常简单。 
首先我们需要将该Python项目打包成exe,在参考了下面两篇博客(博客1、博客2)后采用PyInstaller包进行打包。 
安装好Pyinstaller后进入项目所在目录,运行

pyinstaller -F -w Main.py

这里Main.py替换成你的项目入口,这些参数可以上pyinstaller官网上查。

为生成的exe文件创建一个快捷方式,将该快捷方式放入

C:\Users\XXX\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup

目录中,其中XXX是你的用户名,这样就可以实现开机启动了!

运行结果

功能测试

今天早上在食堂我利用手机微信发送邮件给我的监听邮箱,过了一会就收到了回信,如下图所示: 
发邮件自动回复本机IP——python版本_第1张图片

资源消耗测试

前一天晚上12:23时CPU占用率为0 内存占用为8 磁盘读写和网络都为0 
第二天早上8:26 CPU占用率为0 内存占用为7.6MB 磁盘读写和网络都为0

可以看出来其实该工具占用的资源并不多

总结及未来工作目标

做一下总结吧,虽然已有一些应用能直接用手机远程操控电脑,但终究是要记密码等信息,还要经常开着,而且不能根据我们的要求深度定制,因此我这个小工具还是很不错的 o( ̄▽ ̄)ブ !!

下一步还想添加截图发送的功能,还有邮件重启爬虫的功能,应该都不难,如果有最新消息一定通知大家!!

你可能感兴趣的:(发邮件自动回复本机IP——python版本)