代码如下,配合上window的计划任务,够用了,有心情的,可以猜猜苦逼的程序员几点上班
import urllib
import urllib.request
import http.cookiejar
import calendar,time, datetime
import sys
import random
class Login_kp:
def __init__(self):
cj = http.cookiejar.CookieJar()
self.opener = urllib.request.build_opener( urllib.request.HTTPCookieProcessor(cj))
urllib.request.install_opener(self.opener)
self.opener.addheaders = [('User-agent', 'IE')]
def login(self, username, password):
url = 'http://ip:port/logincheck.php'
data = urllib.parse.urlencode({'UNAME':username, 'PASSWORD':password}).encode('gbk')
req = urllib.request.Request(url, data)
try:
fd = self.opener.open(req)
s = fd.read()
ss = s.decode('gbk')
#print(ss)
result = ss.count("正在进入OA系统")
#print(result)
if result <= 0:
print(u"用户名跟密码不匹配!")
return False
except Exception as e:
print('net err')
return False
fd.close()
print(u'%s login success,ready sign... ' % username),
return True
def logout(self):
url = 'http://ip:port/general/relogin.php'
req = urllib.request.Request(url)
fd = self.opener.open(req)
fd.close()
print(u"quit normally!")
def presign(self):
today = datetime.date.today()
restDay = [calendar.SATURDAY, calendar.SUNDAY]
todayWeek = today.weekday()
if todayWeek in restDay:
print(u"it is rest day,no need to sign!")
return False
else:
sTime = datetime.datetime.now()
now_hour = sTime.hour
now_minute = sTime.minute
print(now_hour,now_minute)
if(now_hour == 7 and now_minute >50):
print("yes, it is time to sign the first")
return True
elif(now_hour > 8 and now_hour < 17):
print("it is too late,the first sign is over")
return False
elif(now_hour == 17 and now_minute > 40):
print("yes, it is time to sign the last")
return True
def sign(self):
sTime = datetime.datetime.now()
now_hour = sTime.hour
now_minute = sTime.minute
if(now_hour < 8):
url = 'http://ip:port/general/attendance/personal/duty/submit.php?REGISTER_TYPE=1'
print(url)
randomAm = random.randint(1,4)
time.sleep(randomAm * 60)
elif(now_hour == 17 and now_minute > 40):
url = 'http://ip:port/general/attendance/personal/duty/submit.php?REGISTER_TYPE=4'
print(url)
randomPm = random.randint(1,2)
time.sleep(randomPm * 60)
else:
exit(0)
sTime = datetime.datetime.now()
now_hour = sTime.hour
now_minute = sTime.minute
req = urllib.request.Request(url)
fd = self.opener.open(req)
s = fd.read()
str = s.decode('gbk')
#print(str)
kqCount = str.count("已考勤")
if kqCount == 1 :
print("Morning sign successful!")
elif kqCount == 2:
print("Afternoon sign successful!")
else:
print("sign fail!")
fd.close()
if __name__ == '__main__':
l = Login_kp()
if l.login('你的用户名', '你的密码') == False:
exit(1)
else:
if l.presign() == True:
l.sign()
l.logout()
else:
exit(0)