importsubprocessimportjsonimportsignalimportosimportdatetimetm=datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')# 得到刷票程序pidchk_pid=subprocess.getoutput("ps aux | grep python3\ /root/shuapiao/shua.py | grep -v grep | awk \'{print $2}\'")# 获取所有参赛项目票数key:value=项目编号,票数cmd='curl "此处省略get字段, 可自行利用浏览器debug功能分析得到"'result=json.loads(subprocess.check_output(cmd,shell=True).decode('utf-8'))['map']res=[]# 获取自己票数myown=int(result['我司id'])forkeyinresult:# 截取选票数res.append(int(result[key]))# 将票数从大到小排序res.sort(reverse=True)print(tm,"The top four votes:",res[:4])ifmyown<res[0]:ifmyown==res[1]:ifchk_pid!="":print(tm,"already on the second position, kill shua.py!")os.kill(int(chk_pid),signal.SIGTERM)else:print(tm,"already on the second position, do nothing!")elifchk_pid=="":print(tm,"not on the second position, run shua.py!")subprocess.call("nohup python3 /root/shuapiao/shua.py > /dev/null &",shell=True)else:print(tm,"not on the second position yet!",myown)elifchk_pid!="":print(tm,"oops! pass the first position, kill shua.py!")os.kill(int(chk_pid),signal.SIGTERM)