PodSixNet模塊

PodSixNet模塊

來自專欄我學python

  • 介紹

PodSixNet模塊是Chris McCormick創建的一個簡單、輕量級的Python網路庫。PodSixNet能讓玩家與伺服器程序通信。使用PodSixNet能容易地完成網路操作。

PodSixNet的python3版本在官方網站

  • 使用說明

python3版本下載地址chr15m/PodSixNet,下載後將PodSixNet文件放在自己項目文件中

  • 模板代碼

server.py

First, you have to tell the server which port it should listen on:

import PodSixNet, timefrom time import sleepfrom PodSixNet.Channel import Channelfrom PodSixNet.Server import Serverclass ClientChannel(Channel): def Network(self, data): print dataclass MyServer(Server): channelClass = ClientChannel def __init__(self, *args, **kwargs): Server.__init__(self, *args, **kwargs) def Connected(self, channel, addr): print "new connection:", channel# use the localaddr keyword to tell the server to listen on port 1337myserver = MyServer(localaddr=(localhost, 1337))while True: myserver.Pump() sleep(0.0001)

client.py

Then, the client has to connect to this port:

import PodSixNet, timefrom PodSixNet.Connection import ConnectionListener, connectionfrom time import sleepclass MyNetworkListener(ConnectionListener): def __init__(self, host, port): self.Connect((host, port)) # 這裡用ConnectionListener實例調用connect def Network(self, data): print data# tell the client which server to connect togui = MyNetworkListener(localhost, 1337)while 1: connection.Pump() gui.Pump()

在client.py中發送數據時用connection.Send({})

connection.Send({"action": "place", "x":xpos, "y":ypos, "is_horizontal": is_horizontal, "gameid": self.gameid, "num": self.num})

推薦閱讀:

谷歌 I/O 大會 2018 前瞻
如何評價HW前HR疑似刷票?
用電流刺激肌肉生長的 SmartMio 可信嗎?
為什麼表情圖片只有動態的沒有語音的?如果能把語音結合在一起那一定很有趣。
為什麼說虛擬現實不是遊戲的最終形態?

TAG:Python | 科技 | 計算機視覺 |