该测试主要目的是为了做自动发包获取相应服务器数据。数据全部采用HTTP的post方法传输,因此可以作为一个基础练手。
主要分为两大部分,储存用的json文件(用的是D:/Documents/SAVE_DATA.json,可在py脚本中修改路径),以及主要进行发包的Python脚本文件。
Json记录
{
"user": "账号",
"PW": "密码",
"Version": "游戏版本",
"LastTime": 0,
"TimeGap": 3600,
"TaskID": 3,
"GroupCnt": 6,
"Ships": [
[
"抓包得到远征用成员ID",
"抓包得到远征用成员ID",
...
],
...
]
}
为了配合发包,需要保留数据。包括:登录用数据"user"“PW”“GamePath”,计时数据"LastTime"“TimeGap”,发包内容"TaskID"“GroupCnt”“Ships”。其中"Ships"是一个数组包含另一组数组,外层数组元素数量应该等于GroupCnt。
抓包得到远征用成员ID应该在原程序正常运行时通过Fiddler等工具进行抓包获取。在此处不加赘述。
Python脚本
import json
import requests
import os
import time
import pyautogui
BotCheck=0
# Reading data back
with open("D:/Documents/PTB_Expedition.json", "r") as f:
SAVE_DATA = json.load(f)
#print(SAVE_DATA)
HistoryRequestData = {"user": SAVE_DATA["user"], "password": SAVE_DATA["PW"],"type":"working"}
LoginRequestData = {"user": SAVE_DATA["user"], "password": SAVE_DATA["PW"],"version":SAVE_DATA["Version"],"device":"dapaofeima000shabidenglujizhi000yuchun00"}
OnlineRequestData = {
"user": SAVE_DATA["user"],
"password": SAVE_DATA["PW"],
"nickname":"dapao_smjbwy",
"behavior":"%e6%b8%b8%e6%88%8f%e7%99%bb%e5%bd%95%e4%b8%ad",
"timestamp":0,
"active":"true",
"DeviceID":"WindowsPlayer_dapaofeima000shabidenglujizhi000yuchun00"}
#os.system("pause")
def main():
#os.system("pause")
#当前已经发送的远征
headers = {
'User-Agent': 'UnityPlayer/2018.4.2f1 (UnityWebRequest/1.0, libcurl/7.52.0-DEV)'
}
History = requests.post("http://bsc.pescn.cc/expedition/history/", data=HistoryRequestData,headers=headers)
print(History.status_code)
HistoryJson=History.json()
print("")
print("History")
print(HistoryJson)
#os.system("pause")
#判断数量
if(HistoryJson["success"]==SAVE_DATA["GroupCnt"]):
#模拟登录
print("")
print("Login")
Login= requests.post("http://bsc.pescn.cc/login/", data=LoginRequestData,headers={'User-Agent':""})
print(Login.json())
print("")
print("Online")
OnlineRequestData["timestamp"]=round(time.time())
OnlineRequest= requests.post("http://bsc.pescn.cc/online/", data=OnlineRequestData,headers={'User-Agent':""})
print(OnlineRequest.json())
os.system("pause")
#接受远征
print("All Back")
HistoryItems=HistoryJson.get("histories")
#print(HistoryItems)
for HistoryItem in HistoryItems:
print(HistoryItem["id"])
ReceiveRequestData={"user":SAVE_DATA["user"], "password": SAVE_DATA["PW"],"history_id":HistoryItem["id"]}
Receive=requests.post("http://bsc.pescn.cc/expedition/receive/", data=ReceiveRequestData,headers=headers)
print("")
print("Receive")
print(Receive.json())
#os.system("pause")
#发送远征
for i in range(SAVE_DATA["GroupCnt"]):
GroupShips=SAVE_DATA["Ships"][i]
GroupShipsStr=",".join(map(str,GroupShips))
SendRequestData={
"user": SAVE_DATA["user"],
"password": SAVE_DATA["PW"],
"task_id": SAVE_DATA["TaskID"],
"ship_ids":GroupShipsStr
}
#print(SendRequestData)
Send=requests.post("http://bsc.pescn.cc/expedition/send/", data=SendRequestData,headers={'User-Agent':""})
SendJson=Send.json()
print("")
print("Send")
print(SendJson)
if(SendJson["status"]!="success"):
BotCheck=1
#os.system("pause")
#保存时间
print("Bot Check: "+str(BotCheck))
with open("D:/Documents/PTB_Expedition.json", "w") as wf:
SAVE_DATA["LastTime"]=time.time()
json.dump(SAVE_DATA,wf)
#重新打印已经发送的远征
History = requests.post("http://bsc.pescn.cc/expedition/history/", data=HistoryRequestData,headers=headers)
HistoryJson=History.json()
print("")
print("History")
print(HistoryJson)
#os.system(r"taskkill /F /IM 工艺战舰Alpha.exe")
#主循环
while (BotCheck==0):
CurrTimeGap=time.time()-SAVE_DATA["LastTime"]
print("启动剩余时间:"+str(SAVE_DATA["TimeGap"]-CurrTimeGap))
if(CurrTimeGap>SAVE_DATA["TimeGap"]+30):
main()
if(BotCheck!=0):
break
time.sleep(60)
先从文件读取json内容。
整体上,发包方式采用最常用的requests.post()。需要有3种发包,分别向/expedition/history/,/login/,/online/,/expedition/receive/,/expedition/send/发送post请求。
- 第一步,向/expedition/history/发送请求,以获取当前的Expedition完成情况。返回值包括:成功编队数量、编队代码等。用成功编队数量与记录中的GroupCnt对比,若相等则全部成功。
- 第二步,向/login/发送请求,进行登录操作,同时向/online/发送心跳请求,确认登录。
- 第三步,向/expedition/receive/发送请求,以回收Expedition。由于有关于登录相关的数据包,若再全部依靠Python脚本实现,会过于麻烦,且缺乏可复用性,因此这里启动了原程序,完成登录相关事宜。后向/expedition/receive/发送请求,相应内容意义不大。
- 第四步,向/expedition/send/发送请求,以发送Expedition。由于发送内容包括编队中的成员,因此需要在Json中记录每个编队与成员的信息。同时,发送的字典中"ship_ids":GroupShipsStr,该value为一个字符串,而记录中是一个数组,因此需要将数组转化为字符串,GroupShipsStr=",".join(map(str,GroupShips))。
- 发送完成后,若存在人机验证相关问题,则会在返回值中得到体现,SendJson[“status”]!=“success”。此时修改一个变量BotCheck,以记录该问题。
- 关闭原程序。
- 最后重新向/expedition/history/发送请求,以确认成功完成了发包的功能。
上述为主函数内容,需要有一个主循环来重复调用主函数。用while判断BotCheck的值,用以决定是否继续运行主函数。因为不保存BotCheck至json中,当该python脚本重新启动时,BotCheck=0。
结语
通过该发包脚本,将原本3~5分钟的手动操作过程,减少到只有30s。若后续继续改进登录相关工作,则可以减少到1s内完成。