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': '局域网局域网', '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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 204,590评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 86,808评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 151,151评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,779评论 1 277
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,773评论 5 367
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,656评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,022评论 3 398
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,678评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 41,038评论 1 299
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,659评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,756评论 1 330
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,411评论 4 321
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,005评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,973评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,203评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,053评论 2 350
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,495评论 2 343

推荐阅读更多精彩内容