Python3 自动登录 签到

 

代码如下,配合上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)

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值