Python游戲接口請求實例

Method name Method interpretation
init() Constructor initialization property
CrateUser() Create user method
GameLogin() Login user method
PlayGame() Business interface request method
GameOver() Break link method

●The code is as follows:

#!/usr/bin/python
# -*- coding: utf-8 -*-

#@Users: LiMu
#@Files:MM3TestTools.py
#@Times: 2022/9/30 
#@Software:PyCharm

import os
import sys
import time
import uuid
import json
import websocket
from jsonpath import jsonpath

class ConnectWebsocket(object):
    #Constructor initialization properties
    def __init__(self,Url,ServerId):
        self.ConnectServer = websocket.create_connection(Url)
        self.ServerId = ServerId

    #Create user interface request
    def CrateUser(self,UserName: str):
        #Create user parameters
        crate_parameters = {"HandleCode": str(uuid.uuid4()).replace("-", ""), "ModuleName": "Player",
                                 "MethodName": "LoginYaCeName", "PartnerId": "1001", "ServerId": self.ServerId,
                                 "PlayerId": "", "Session": "", "GameVersionId": "1212121",
                                 "ResourceVersionName": "2232a", "Data": [UserName,"0"]}

        #Send parameters to the server
        self.ConnectServer.send(json.dumps(crate_parameters))

        while True:
            #Accept the returned information and process it
            recvs = self.ConnectServer.recv()
            response = json.loads(recvs)

            # Filter the request information returned
            if response.get("HandleCode") == crate_parameters.get("HandleCode"):
                self.playerid = jsonpath(response, "$.Value.PlayerId")[0]
                self.sessionid = jsonpath(response, "$.Value.Session")[0]
                print("Crate request parameters:{}".format(crate_parameters))
                print("Crate return information:{}".format(response))
                print("=" * 160)
                break

    #Request method for game login interface
    def GameLogin(self,UserName:str):
        #Assembly login parameters
        login_parameters = {"HandleCode": str(uuid.uuid4()).replace("-",""),"ModuleName": "Player",
                                 "MethodName": "Login_ForDebugTest","PartnerId": "1001","ServerId": self.ServerId,
                                 "PlayerId": "","Session": "","GameVersionId": "1212121",
                                 "ResourceVersionName": "2232a","Data":[UserName]}

        #Send parameters to the server
        self.ConnectServer.send(json.dumps(login_parameters))

        while True:
            #Accept the returned information and process it
            recvs = self.ConnectServer.recv()
            response = json.loads(recvs)

            #Filter the request information returned
            if response.get("HandleCode") == login_parameters.get("HandleCode"):
                self.playerid = jsonpath(response,"$.Value.PlayerId")[0]
                self.sessionid = jsonpath(response,"$.Value.Session")[0]
                print("Login request parameters:{}".format(login_parameters))
                print("Login return information:{}".format(response))
                print("=" * 160)
                break

    #Game service interface request method
    def PlayGame(self,ModuleAndMethod:list,Data:list):
        #Parameters of assembly salesman
        play_parameters = {"HandleCode": str(uuid.uuid4()).replace("-",""),"ModuleName":ModuleAndMethod[0],"MethodName":ModuleAndMethod[1],"PartnerId": "1001","ServerId": self.ServerId,"PlayerId":self.playerid,"Session":self.sessionid,"GameVersionId": "1212121","ResourceVersionName": "2232a","Data":Data}

        #Send parameters to the server
        self.ConnectServer.send(json.dumps(play_parameters))

        while True:
            #Accept the returned information and process it
            recvs = self.ConnectServer.recv()
            response = json.loads(recvs)

            #Filter the request information returned
            if play_parameters.get("HandleCode") == response.get("HandleCode"):
                print("API request parameters:{}".format(play_parameters))
                print("API returns information:{}".format(response))
                print("=" * 160)
                return response

    def GameOver(self):
        self.ConnectServer.close()

if __name__ == '__main__':
    #Link server and player login,1041=6Port,1021=6Auto,1043=6Kuafu,6000=6Dev
    ConnectServer = ConnectWebsocket("ws://10.1.0.85:30018/client","1041")
    ConnectServer.GameLogin("沐歌1號")

    #Front interface service interface instance
    Premise = ConnectServer.PlayGame(["Friend","GetList"],[])
    Business = ConnectServer.PlayGame(["Friend","GiveFriendshipCoin"],[jsonpath(Premise,"$..FriendList..[?(@.FriendPlayerName=='沐歌3號'&&@.Lv==60)].FriendPlayerId")[0]])
    #print("Whether or not to give gifts to friends:",jsonpath(Premise,"$..FriendList..[?(@.FriendPlayerName=='沐歌3號'&&@.Lv==60)].IsGive"))
    #print("Whether to receive a gift or not:",jsonpath(Premise, "$..FriendList..[?(@.FriendPlayerName=='沐歌3號'&&@.Lv==60)].ReceiveType"))

    game_over = ConnectServer.GameOver()

●The returned result is as follows:

D:\Python\python.exe E:\pythonFiles\Interfacetest\InterfaceTest\Lj6TestTools.py 
Login request parameters:{'HandleCode': '2bb293d0072c4516bea56d5201005e79', 'ModuleName': 'Player', 'MethodName': 'Login_ForDebugTest', 'PartnerId': '1001', 'ServerId': '1041', 'PlayerId': '', 'Session': '', 'GameVersionId': '1212121', 'ResourceVersionName': '2232a', 'Data': ['沐歌1號']}
Login return information:{'HandleCode': '2bb293d0072c4516bea56d5201005e79', 'PushKey': None, 'ResultStatus': 0, 'Status': 0, 'StatusMsg': '成功', 'Value': {'PlayerId': '7110128281529090049', 'Session': '7D1C52566A4F6744D34FCCF868FFB0A7', 'IsNewPlayer': False, 'IpHome': '局域網(wǎng)局域網(wǎng)', 'PlayerName': '沐歌1號', 'UserId': '484fd038ee18459896ae0008e4f0b75a', 'HeadImageId': 26002001, 'RegisterTime': 1685344639, 'ServerOpenDays': 33, 'ServerOpenDate': 1682870400, 'ServerStartDate': 1682888400, 'UseModelType': 1, 'IsInnerPlayer': 0, 'ChatPartnerIdStr': '', 'LvChangeTime': 1685345838}, 'TimeTick': 1685687703, 'ExtValue': None}
================================================================================================================================================================
API request parameters:{'HandleCode': 'cbb636c477604537ad1a0bd17a93ab24', 'ModuleName': 'Friend', 'MethodName': 'GetList', 'PartnerId': '1001', 'ServerId': '1041', 'PlayerId': '7110128281529090049', 'Session': '7D1C52566A4F6744D34FCCF868FFB0A7', 'GameVersionId': '1212121', 'ResourceVersionName': '2232a', 'Data': []}
API returns information:{'HandleCode': 'cbb636c477604537ad1a0bd17a93ab24', 'PushKey': None, 'ResultStatus': 0, 'Status': 0, 'StatusMsg': '成功', 'Value': {'FriendList': {'FriendList': [{'FriendPlayerId': '7110269019017445377', 'FriendPlayerName': '沐歌3號', 'Lv': 60, 'Fap': 8427, 'LastOnlineTime': '在線', 'ServerName': '天府之國', 'GuildName': '', 'HeadImageId': 19010000, 'CreateTime': 1685725907, 'ServerId': 1043, 'PartnerId': 1001, 'IsGive': True, 'ReceiveType': 0}]}, 'FriendInfo': {'GiveCount': 1, 'ReceiveCount': 0}}, 'TimeTick': 1685687703, 'ExtValue': None}
================================================================================================================================================================
API request parameters:{'HandleCode': '6a7ffc73b7ac4035b66d559133a9d89b', 'ModuleName': 'Friend', 'MethodName': 'GiveFriendshipCoin', 'PartnerId': '1001', 'ServerId': '1041', 'PlayerId': '7110128281529090049', 'Session': '7D1C52566A4F6744D34FCCF868FFB0A7', 'GameVersionId': '1212121', 'ResourceVersionName': '2232a', 'Data': ['7110269019017445377']}
API returns information:{'HandleCode': '6a7ffc73b7ac4035b66d559133a9d89b', 'PushKey': None, 'ResultStatus': 1115, 'Status': 1115, 'StatusMsg': '今日已贈送過獎勵', 'Value': None, 'TimeTick': 1685687703, 'ExtValue': None}
================================================================================================================================================================

Process finished with exit code 0
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末豌研,一起剝皮案震驚了整個濱河市瓣距,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌窘茁,老刑警劉巖驹碍,帶你破解...
    沈念sama閱讀 219,589評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件壁涎,死亡現(xiàn)場離奇詭異,居然都是意外死亡志秃,警方通過查閱死者的電腦和手機怔球,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,615評論 3 396
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來浮还,“玉大人竟坛,你說我怎么就攤上這事【啵” “怎么了担汤?”我有些...
    開封第一講書人閱讀 165,933評論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長洼冻。 經(jīng)常有香客問我崭歧,道長,這世上最難降的妖魔是什么撞牢? 我笑而不...
    開封第一講書人閱讀 58,976評論 1 295
  • 正文 為了忘掉前任率碾,我火速辦了婚禮,結(jié)果婚禮上普泡,老公的妹妹穿的比我還像新娘播掷。我一直安慰自己审编,他們只是感情好撼班,可當(dāng)我...
    茶點故事閱讀 67,999評論 6 393
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著垒酬,像睡著了一般砰嘁。 火紅的嫁衣襯著肌膚如雪件炉。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,775評論 1 307
  • 那天矮湘,我揣著相機與錄音斟冕,去河邊找鬼。 笑死缅阳,一個胖子當(dāng)著我的面吹牛磕蛇,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播十办,決...
    沈念sama閱讀 40,474評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼秀撇,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了向族?” 一聲冷哼從身側(cè)響起呵燕,我...
    開封第一講書人閱讀 39,359評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎件相,沒想到半個月后再扭,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,854評論 1 317
  • 正文 獨居荒郊野嶺守林人離奇死亡夜矗,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,007評論 3 338
  • 正文 我和宋清朗相戀三年泛范,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片紊撕。...
    茶點故事閱讀 40,146評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡敦跌,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出逛揩,到底是詐尸還是另有隱情柠傍,我是刑警寧澤,帶...
    沈念sama閱讀 35,826評論 5 346
  • 正文 年R本政府宣布辩稽,位于F島的核電站惧笛,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏逞泄。R本人自食惡果不足惜患整,卻給世界環(huán)境...
    茶點故事閱讀 41,484評論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望喷众。 院中可真熱鬧各谚,春花似錦、人聲如沸到千。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,029評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽憔四。三九已至膀息,卻和暖如春般眉,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背潜支。 一陣腳步聲響...
    開封第一講書人閱讀 33,153評論 1 272
  • 我被黑心中介騙來泰國打工甸赃, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人冗酿。 一個月前我還...
    沈念sama閱讀 48,420評論 3 373
  • 正文 我出身青樓埠对,卻偏偏與公主長得像,于是被迫代替她去往敵國和親裁替。 傳聞我的和親對象是個殘疾皇子鸠窗,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,107評論 2 356

推薦閱讀更多精彩內(nèi)容