1、购买阿里云服务器
https://www.aliyun.com/product/ecs
2、安装宝塔
yum install -ywget && wget-O install.sh http://download.bt.cn/install/install_6.0.sh && sh install.sh ed099927
3、编写脚本
①导入包
importrequestsimportjsonimportosimporttimeimportsmtplibfromemail.mime.textimportMIMETextfromemail.utilsimportformataddr
②请求登录接口
deflogin(): try: url="######"payload=json.dumps({ "username": username, "password": password }) headers= { 'Host': 'api.xchanger.cn', 'Cookie': '###########', 'accept': 'application/json;responseformat=3', 'content-type': 'application/json', 'x-app-id': '##', 'user-agent': 'BackgroundShortcutRunner/1092.8.7 CFNetwork/1220.1 Darwin/20.3.0', 'accept-language': 'zh-cn' } response=requests.request("POST", url, headers=headers, data=payload,verify=False) print(response.json()) accessToken=response.json()['data']['accessToken'] userId=response.json()['data']['userId'] withopen(r'login.txt', "w+") asf1: f1.write(accessToken) withopen(r'userId.txt', "w+") asf2: f2.write(userId) returnaccessTokenexceptExceptionasDamnMistake: print(DamnMistake) mail("登录错误",str(DamnMistake))
③请求车辆状态接口
defstatus(sessionToken): ifos.path.exists(r'userId.txt'): withopen(r'userId.txt', "r") asf: userId=f.read() else: userId="#####"url="#####"+vin+"#####"+userIdpayload= {} headers= { 'Host': 'api.xchanger.cn', 'Cookie': '#######', 'accept': 'application/json;responseformat=3', 'ecarx-net-platform': 'tsp', 'user-agent': '#########)', 'authorization': sessionToken, 'accept-language': 'zh-cn' } response=requests.request("GET", url, headers=headers, data=payload,verify=False) try: #数据更新日期updateTime=response.json()['data']['vehicleStatus']['updateTime'] updateTime=time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(response.json()['data']['vehicleStatus']['updateTime']/1000)) #左前车窗(2是关闭)winStatusDriver=response.json()['data']['vehicleStatus']['additionalVehicleStatus']['climateStatus']['winStatusDriver'] #右前车窗(2是关闭)winStatusPassenger=response.json()['data']['vehicleStatus']['additionalVehicleStatus']['climateStatus']['winStatusPassenger'] #左后车窗(2是关闭)winStatusDriverRear=response.json()['data']['vehicleStatus']['additionalVehicleStatus']['climateStatus']['winStatusDriverRear'] #右后车窗(2是关闭)winStatusPassengerRear=response.json()['data']['vehicleStatus']['additionalVehicleStatus']['climateStatus']['winStatusPassengerRear'] #冷却液是否正常(ture是正常)engineCoolantTemperatureValidity=response.json()['data']['vehicleStatus']['additionalVehicleStatus']['runningStatus']['engineCoolantTemperatureValidity'] #后尾箱是否开启(1开启,0没有开启)trunkOpenStatus=response.json()['data']['vehicleStatus']['additionalVehicleStatus']['drivingSafetyStatus']['trunkOpenStatus'] #引擎盖是否开启(1开启,0没有开启)engineHoodOpenStatus=response.json()['data']['vehicleStatus']['additionalVehicleStatus']['drivingSafetyStatus']['engineHoodOpenStatus'] #左前门打开状态(0是关闭)doorOpenStatusDriver=response.json()['data']['vehicleStatus']['additionalVehicleStatus']['drivingSafetyStatus']['doorOpenStatusDriver'] #右前门打开状态(0是关闭)doorOpenStatusPassenger=response.json()['data']['vehicleStatus']['additionalVehicleStatus']['drivingSafetyStatus']['doorOpenStatusPassenger'] #左后门打开状态(0是关闭)doorOpenStatusDriverRear=response.json()['data']['vehicleStatus']['additionalVehicleStatus']['drivingSafetyStatus']['doorOpenStatusDriverRear'] #右后门打开状态(0是关闭)doorOpenStatusPassengerRear=response.json()['data']['vehicleStatus']['additionalVehicleStatus']['drivingSafetyStatus']['doorOpenStatusPassengerRear'] #左前门门锁状态(1是已上锁)doorLockStatusDriver=response.json()['data']['vehicleStatus']['additionalVehicleStatus']['drivingSafetyStatus']['doorLockStatusDriver'] #左后门门锁状态(1是已上锁)doorLockStatusDriverRear=response.json()['data']['vehicleStatus']['additionalVehicleStatus']['drivingSafetyStatus']['doorLockStatusDriverRear'] #右前门门锁状态(1是已上锁)doorLockStatusPassenger=response.json()['data']['vehicleStatus']['additionalVehicleStatus']['drivingSafetyStatus']['doorLockStatusPassenger'] #右后门门锁状态(1是已上锁)doorLockStatusPassengerRear=response.json()['data']['vehicleStatus']['additionalVehicleStatus']['drivingSafetyStatus']['doorLockStatusPassengerRear'] #车辆报警是否开启(1为已开启)vehicleAlarm=response.json()['data']['vehicleStatus']['additionalVehicleStatus']['drivingSafetyStatus']['vehicleAlarm'] #电瓶电压voltage=response.json()['data']['vehicleStatus']['additionalVehicleStatus']['maintenanceStatus']['mainBatteryStatus']['voltage'] #发动机状态(engine_off为关闭,engine_running为开启)engineStatus=response.json()['data']['vehicleStatus']['basicVehicleStatus']['engineStatus'] carStatus_dict= { "数据更新时间": updateTime, "左前车窗": winStatusDriver, "右前车窗": winStatusPassenger, "左后车窗": winStatusDriverRear, "右后车窗": winStatusPassengerRear, "后尾箱是否开启": trunkOpenStatus, "引擎盖是否开启": engineHoodOpenStatus, "左前门打开状态": doorOpenStatusDriver, "右前门打开状态": doorOpenStatusPassenger, "左后门打开状态": doorOpenStatusDriverRear, "右后门打开状态": doorOpenStatusPassengerRear, "左前门门锁状态": doorLockStatusDriver, "左后门门锁状态": doorLockStatusDriverRear, "右前门门锁状态": doorLockStatusPassenger, "右后门门锁状态": doorLockStatusPassengerRear, "车辆报警是否开启": vehicleAlarm, "电瓶电压": voltage, "发动机状态": engineStatus } withopen(r'carStatus_dict.txt', "w+") asf: f.write(str(carStatus_dict)) returncarStatus_dictexceptExceptionasDamnMistake: print(DamnMistake) mail("获取各项数据错误",str(DamnMistake))
⑤发送邮件
defmail(msgTitle, result_json): my_sender='####@qq.com'# 发件人邮箱账号my_pass='####'# 发件人邮箱密码my_user='#####@qq.com'# 收件人邮箱账号try: msg=MIMEText(json.dumps(result_json, sort_keys=True, indent=4, separators=(',', ':'), ensure_ascii=False), 'plain', 'utf-8') msg['From'] =formataddr(["###", my_sender]) # 括号里的对应发件人邮箱昵称、发件人邮箱账号msg['To'] =formataddr(["FK", my_user]) # 括号里的对应收件人邮箱昵称、收件人邮箱账号msg['Subject'] =msgTitle# 邮件的主题server=smtplib.SMTP_SSL("smtp.qq.com", 465) # 发件人邮箱中的SMTP服务器,端口是25server.login(my_sender, my_pass) # 括号中对应的是发件人邮箱账号、邮箱密码server.sendmail(my_sender, my_user, msg.as_string()) # 括号中对应的是发件人邮箱账号、收件人邮箱账号、发送邮件server.quit() # 关闭连接exceptExceptionasfuckError: # 如果 try 中的语句没有执行,则会执行下面的print('发送邮件报错了,报错内容为:\n') print(fuckError)
⑥主程序
defmain(): sessionToken=update_token() carStatus_dict=status(sessionToken) print(carStatus_dict) car_Alarm=[] ifcarStatus_dict['发动机状态'] =="engine_off": ifcarStatus_dict['左前车窗'] !="2": car_Alarm.append("左前车窗未关闭") ifcarStatus_dict['右前车窗'] !="2": car_Alarm.append("右前车窗未关闭") ifcarStatus_dict['左后车窗'] !="2": car_Alarm.append("左后车窗未关闭") ifcarStatus_dict['右后车窗'] !="2": car_Alarm.append("右后车窗未关闭") ifcarStatus_dict['后尾箱是否开启'] =="1": car_Alarm.append("后尾箱未关闭") ifcarStatus_dict['左前门门锁状态'] !="1": car_Alarm.append("左前门未锁") ifcarStatus_dict['左后门门锁状态'] !="1": car_Alarm.append("左后门未锁") ifcarStatus_dict['右前门门锁状态'] !="1": car_Alarm.append("右前门未锁") ifcarStatus_dict['右后门门锁状态'] !="1": car_Alarm.append("右后门未锁") ifcarStatus_dict['左前门打开状态'] =="1": car_Alarm.append("左前门未关闭") ifcarStatus_dict['左后门打开状态'] =="1": car_Alarm.append("左后门未关闭") ifcarStatus_dict['右前门打开状态'] =="1": car_Alarm.append("右前门未关闭") ifcarStatus_dict['右后门打开状态'] =="1": car_Alarm.append("右后门未关闭") print(car_Alarm) else: passiflen(car_Alarm)>0: car_Alarm.append("数据更新时间:"+carStatus_dict['数据更新时间']) withopen(r'car_Alarm.txt', "w+") asf1: f1.write(str(car_Alarm)) mail("车辆报警!!!!!!", car_Alarm)
4、上传脚本
①打开宝塔
②点击“文件”
③上传
5、开启定时任务
①打开宝塔
②点击“计划任务”
6、结束