Skip to content

Instantly share code, notes, and snippets.

@CORDEA CORDEA/server.py
Created Jun 30, 2014

Embed
What would you like to do?
#!/bin/env/python
from PodSixNet.Channel import Channel
from PodSixNet.Server import Server
from time import sleep
class NewChannel(Channel):
def __init__(self, *args, **kwargs):
Channel.__init__(self, *args, **kwargs)
def Network_getHello(self, data):
print("network: hello")
message = data["message"]
self._server.allSendBye(message)
def Network_getBye(self, data):
print("network: bye")
message = data["message"]
self._server.allSendHello(message)
class NewServer(Server):
channelClass = NewChannel
def __init__(self, *args, **kwargs):
Server.__init__(self, *args, **kwargs)
self.clients = {}
self.clientNumber = 0
print("Server launched")
def Connected(self, channel, addr):
print("New connection: " + str(channel))
self.clients[self.clientNumber] = channel
self.clientNumber += 1
def allSendHello(self, message):
print("client message: " + message)
clientList = self.clients.values()
for i in clientList:
i.Send({"action": "getHello", "message": "Hello Client!"})
def allSendBye(self, message):
print("client message: " + message)
clientList = self.clients.values()
for i in clientList:
i.Send({"action": "getBye", "message": "Bye Client!"})
def launch(self):
while True:
self.Pump()
sleep(0.0001)
if __name__=='__main__':
address = raw_input("Host:Port : ")
if not address:
host, port = "localhost", 8000
else:
host, port = address.split(":")
newServer = NewServer(localaddr=(host, int(port)))
newServer.launch()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.