
Python 面向对象基础部分
中秋在家没事, 写了一个很久以前就想写的脚本
如果下午 6 点 10 分还连接着公司的 wifi, 就发邮件给老婆说要加班
为什么要发邮件而不发短信呢, 因为短信接口要钱….
最近买了个 4k 的显示器, 拿来外接 mac 看代码, 爽翻了, 不过有点蛮烦的就是每次都要拖动鼠标到另一个屏幕上去, 不过全国最大的同性交友网站 GitHub
上面有一款开源的软件叫 CatchMouse 解决了这个问题
下载地址: CatchMouse
另一个问题是如果想把软件从 mac 屏幕放到外接显示屏的话, 还是要拖过去, 但是….
另一款神器 Moon 能帮我们快速的把当前应用移动到外接显示器上,

光标定位到需要移动的 app 上, 快捷键 contro+` 即可
接下来是脚本
我的思路是:
先检测当前连接的是哪里的 wifi
如果是公司的 wifi, 且当前时间大于 6 点 10 分, 则给老婆发送邮件
如果连接的是家里的 wifi, 则检测是否连接了外接显示器,
如果连接了, 则检测是否开启了 CatchMouse.app, 没有则打开.
接下来开始撸代码:
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 
 | 
 
 
 
 
 
 import socket
 import subprocess
 import datetime
 import smtplib
 import time
 from email.mime.text import MIMEText
 from email.header import Header
 
 mail_host = "smtp.163.com"
 mail_user = "用户名"
 mail_pass = "密码"
 mail_postfix = "163.com"
 log_dir = "smartlife.log"
 
 
 global_flag = True
 
 def whereami(flag):
 myname = socket.getfqdn(socket.gethostname())
 save_log(myname)
 myaddr = socket.gethostbyname(myname)
 
 count_spring = myaddr.count("192.168.31.")
 count_hiwifi = myaddr.count("192.168.199.")
 
 if count_hiwifi + count_spring == 1:
 save_log("家里的wifi")
 
 display()
 else:
 save_log("公司的wifi")
 now_time = datetime.datetime.now()
 save_log("当前小时是 %s" % now_time.hour)
 save_log("当前分钟是 %s" % now_time.minute)
 if now_time.hour == 18 and now_time.minute >= 10 and flag:
 send_mail(['269321381@qq.com'], '来自老公的邮件', "老婆 我要加班,你先吃")
 global global_flag
 global_flag = False
 
 
 def display():
 
 out_bytes = subprocess.check_output("ioreg -l | grep 'DisplayProductID'", shell = True)
 str1 = str(out_bytes, encoding = "utf-8")
 count = str1.count("DisplayProductID")
 str2 = str(subprocess.check_output("ps -ef | grep CatchMouse.app | grep -v grep | awk '{ print $2 }'", shell = True),
 encoding = "utf-8")
 save_log("CatchMouse.app pid: " + str2)
 
 if count == 2 and str2 == "":
 save_log("开启CatchMouse")
 retcode = subprocess.call("open -a CatchMouse.app", shell = True)
 if retcode == 0:
 log = " 开启CatchMouse.app成功"
 else:
 log = " 开启CatchMouse.app失败"
 save_log(log)
 
 
 def send_mail(to_list, title, context):
 me = mail_user + "<" + mail_user + "@" + mail_postfix + ">"
 msg = MIMEText(context, 'plain', 'utf-8')
 msg['Subject'] = Header(title, 'utf-8')
 msg['From'] = me
 msg['To'] = ";".join(to_list)
 try:
 s = smtplib.SMTP()
 s.connect(mail_host)
 s.login(mail_user, mail_pass)
 s.sendmail(me, to_list, msg.as_string())
 s.quit()
 save_log("邮件发送成功")
 except Exception as e:
 save_log(str(e))
 
 
 def save_log(log_str):
 write_log = open(log_dir, 'a')
 log = '[%s]--> %s\n' % (time.strftime('%Y-%m-%d %H:%M:%S'), log_str)
 print(log)
 write_log.write(log)
 write_log.close()
 
 if __name__ == '__main__':
 while (True):
 whereami(global_flag)
 time.sleep(10)
 
 | 
脚本完成了
受到 超过 90 秒的任务不自动化, 你好意思说自己是黑客? 这篇文章的启发, 所有有了这个脚本
灵感来源于生活…….