1
2
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
| import base64
import configparser
import http.client
import msvcrt
import os
import pycurl
import smtplib
import time
from email.mime.text import MIMEText
from urllib.parse import urlencode
class Cookies:
def __init__(self):
self.url = ''
self.username = ''
self.password = ''
self.status = None
self.cookies = []
self.user_id = None
def _body(self, buf):
# drop body info
with open('nul', 'w') as fh:
print(buf, file=fh)
def _store(self, buf):
if isinstance(buf, bytes):
buf = buf.decode('gb2312')
if buf.startswith('HTTP/1.1'):
# 获取HTTP Status Code
self.status = buf.split(' ')[1]
elif buf.startswith('Set-Cookie:'):
# 获取 Cookie
item = buf.strip().split(': ')[-1].split(';')[0]
self.cookies.append(item)
if item.startswith('SID_'):
# 从 Cookie 中截取 user_id
self.user_id = item.split("=")[0].split('_')[-1]
def get(self):
post_data = {
'UNAME': self.username,
'PASSWORD': self.password
}
# Form data must be provided already urlencoded.
post_fields = urlencode(post_data)
c = pycurl.Curl()
c.setopt(c.URL, 'http://{}/logincheck.php'.format(self.url))
c.setopt(c.WRITEFUNCTION, self._body)
# save header info
c.setopt(c.HEADERFUNCTION, self._store)
# Sets request method to POST,
# Content-Type header to application/x-www-form-urlencoded
# and data to send in request body.
c.setopt(c.POSTFIELDS, post_fields)
c.perform()
c.close()
class Mail:
def __init__(self):
self.is_send = False
self.send_to = ""
self.host = ""
def send(self):
if not self.is_send:
# 第三方 SMTP 服务
mail_host = "smtp.126.com" # SMTP服务器
mail_user = "xxx" # 用户名
mail_pass = "xxx" # 密码
sender = 'xxx@126.com' # 发件人邮箱(最好写全, 不然会失败)
title = 'OA有新消息, 请注意查收' # 邮件主题
content = '点我直达: http://{} \n\n有问题可以在云之家@贾大空'.format(self.host)
message = MIMEText(content, 'plain', 'utf-8') # 内容, 格式, 编码
message['From'] = "{}".format(sender)
message['To'] = self.send_to
message['Subject'] = title
try:
smtpObj = smtplib.SMTP_SSL(mail_host, 465) # 启用SSL发信, 端口一般是465
smtpObj.login(mail_user, mail_pass) # 登录验证
smtpObj.sendmail(sender, self.send_to, message.as_string()) # 发送
self.is_send = True
print("检测到新消息, 邮件通知已发送")
except smtplib.SMTPException:
print("通知邮件发送失败, 五分钟后再试")
def pwd_input():
"""输入密码替换为星号
:return: 字符串形式密码
"""
chars = []
while True:
try:
new_char = msvcrt.getch().decode(encoding="utf-8")
except:
return input("你很可能不是在cmd命令行下运行,密码输入将不能隐藏:")
if new_char in '\r\n': # 如果是换行,则输入结束
break
elif new_char == '\b': # 如果是退格,则删除密码末尾一位并且删除一个星号
if chars:
del chars[-1]
msvcrt.putch('\b'.encode(encoding='utf-8')) # 光标回退一格
msvcrt.putch(' '.encode(encoding='utf-8')) # 输出一个空格覆盖原来的星号
msvcrt.putch('\b'.encode(encoding='utf-8')) # 光标回退一格准备接受新的输入
else:
chars.append(new_char)
msvcrt.putch('*'.encode(encoding='utf-8')) # 显示为星号
print('')
return ''.join(chars)
def check_sms(host, user_id, cookie):
conn = http.client.HTTPConnection(host)
headers = {
'user-agent': "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36",
'accept': "*/*",
'accept-encoding': "gzip, deflate, sdch",
'accept-language': "zh-CN,zh;q=0.8",
'cookie': cookie,
}
try:
conn.request("GET", "/attachment/new_sms/{}.sms?now={}".format(user_id, time.time()), headers=headers)
except ConnectionRefusedError:
print("查询动作被服务器拒绝")
return "0"
except TimeoutError:
print("服务器超时")
return "0"
res = conn.getresponse()
data = res.read()
return data.decode("gb2312")
def read_settings(mail, ck, cf):
if cf.get("main", "send_to"):
mail.send_to = cf.get("main", "send_to")
else:
mail.send_to = input("请输入你的邮箱地址: ").strip()
cf.set("main", "send_to", mail.send_to)
if cf.get("main", "host"):
ck.url = cf.get("main", "host")
else:
ck.url = input('请输入OA地址(不要带"http://:"): ').strip()
cf.set("main", "host", ck.url)
if cf.get("main", "username"):
ck.username = cf.get("main", "username")
else:
ck.username = input('请输入OA用户名: ').strip()
cf.set("main", "username", ck.username)
if cf.get("main", "password"):
ck.password = base64.b64decode(bytes(cf.get("main", "password"), 'utf8')).decode('utf8')
else:
print("请输入OA密码: ")
ck.password = pwd_input()
cf.set("main", "password", base64.b64encode(bytes(ck.password, 'utf8')).decode('utf8'))
cf.write(open(conf_file, "w"))
def main():
cf = configparser.ConfigParser()
cf.read(conf_file)
mail = Mail()
ck = Cookies()
read_settings(mail, ck, cf)
mail.host = ck.url
while 1:
ck.get() # 获取登录信息
if ck.status == "302": # 登录成功获得的正确响应
break
else:
print("用户名或密码有误, 请重新输入")
# 登录失败, 清空用户名/密码, 并要求充填
cf.set("main", "username", "")
cf.set("main", "password", "")
read_settings(mail, ck, cf)
cookie = '; '.join(ck.cookies) # 拼接cookie
print("开始检测, 将此窗口最小化即可")
try:
while 1:
if check_sms(ck.url, ck.user_id, cookie) != "0":
mail.send()
else:
mail.is_send = False
time.sleep(5 * 60) # sleep 5min
except KeyboardInterrupt:
print("检测到按键中断, 程序将退出")
if __name__ == "__main__":
conf_file = "oa_check.ini"
conf_info = """[main]
; OA网址, 注意不需要开头的http://哦
host = xxx.com
username =
password =
; 你要接收提醒的邮箱, 如qq邮箱 hello@qq.com
send_to =
"""
if not os.path.exists(conf_file):
with open(conf_file, "w") as fh:
fh.write(conf_info)
print("""
_______________
< OA消息提醒工具 >
---------------
\ ^__^
\ (oo)\_______
(__)\ )\/\\-
||----w |
|| ||""")
print("{} by ferstar".format("\t" * 3))
print("")
print("设置好邮箱/用户名/密码后, 如果OA系统有新通知时")
print("")
print("你设置的邮箱会收到一封来自xxx@126.com的邮件提醒")
print("")
main()
|