自己能看懂就行 ^_^
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
__author__ = 'tan9le'
import requests
import datetime
import random
host = '服务器地址+端口'
Origin = 'http://服务器地址+端口/'
indexURL = Origin + 'attp/www/index.html'
loginURL = Origin + 'attp/login/login.do'
operURL = Origin + 'attp/attendance/entryForJsp'
user_name = '用户名'
password = '密码'
today = datetime.date.today()
working_days = ['2016-09-17','2016-10-08','2016-10-09']
holidays = ['2016-10-03','2016-10-04','2016-10-04','2016-10-06','2016-10-07']
#通用header
header_base = {
'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36',
'Host': host,
'Origin':Origin,
'Connection':'keep-alive',
'Referer':indexURL
}
#登录时引用的header
loginHeader = header_base.copy()
loginHeader["Content-Type"] = 'application/json;charset=UTF-8'
#操作时引用的header
operHeader = header_base.copy()
#全局request session,用来解决重复请求时的session、cookeis问题
s = requests.session()
def init():
#得到日期对应的字符串
todaystr = today.strftime("%Y-%m-%d")
#得到日期是星期几,0代表周一
dayofweek = today.weekday()
#判断是否应该打卡
if dayofweek < 6:
if(todaystr in holidays):
operflag = False
print("工作日放假^_^")
else:
operflag = True
print("正常上班,需要打卡")
else:
if(todaystr in working_days):
operflag = True
print("周末正常上班,需要打卡")
else:
operflag = False
print("周末正常休息^_^")
return operflag
#登录请求
def login():
loginHeader['request-type'] = 'angular/api'
login_param = {"login_id":user_name,"password":password,"roleType":"0"}
#登录
s.headers = loginHeader
s.post(loginURL,json=login_param)
#访问初始界面
r = s.get(Origin + 'attp/attendance/initPage')
#登录成功后,将header还原
s.headers = header_base
return r
#出勤
def chuqin(yearmonth,begin_time):
operHeader['Referer'] = Origin + 'attp/attendance/initAttForJsp'
oper_param = {"staff_code":user_name,"yearmonth":yearmonth,"project_id":"-1","projectname":" ","projectcode":"-1","area_id":"南京市","actualArea":"江苏省 南京市","create_user":user_name,"begin_time":begin_time}
s.headers = operHeader
r = s.post(operURL, json=oper_param)
# 操作成功后,将header还原
s.headers = header_base
return r
#退勤
def tuiqin(yearmonth,end_time):
operHeader['Referer'] = Origin + 'attp/attendance/initAttForJsp'
oper_param = {"staff_code":user_name,"yearmonth":yearmonth,"project_id":"-1","projectname":" ","projectcode":"-1","area_id":"南京市","actualArea":"江苏省 南京市","create_user":user_name,"end_time":end_time}
s.headers = operHeader
r = s.post(operURL, json=oper_param)
# 操作成功后,将header还原
s.headers = header_base
return r
#获取HH:MM样式的随机时间
def randmontime(beginHH,endHH,beginMM,endMM):
HH = random.randint(beginHH, endHH)
MM = random.randint(beginMM, endMM)
if(HH<10):
HHstr = '0' + str(HH)
else:
HHstr = str(HH)
if (MM < 10):
MMstr = '0' + str(MM)
else:
MMstr = str(MM)
return HHstr + ':' + MMstr
if __name__ == '__main__':
flag = init()
print(flag)
if flag:
todaystr = today.strftime("%Y-%m-%d")
chuqintime = randmontime(7,8,15,45)
tuiqintime = randmontime(19,20,30,59)
login()
print(chuqin(todaystr,chuqintime).text)
print(tuiqin(todaystr,tuiqintime).text)