Update 'server.py'
This commit is contained in:
180
server.py
180
server.py
@@ -1,91 +1,91 @@
|
|||||||
import socket, sys, datetime, threading, json
|
import socket, sys, datetime, threading, json
|
||||||
|
|
||||||
class Server:
|
class Server:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.version = 1.0
|
self.version = 1.0
|
||||||
print("Server version: {}".format(self.version))
|
print("Server version: {}".format(self.version))
|
||||||
self.motd = "cubey"
|
self.motd = "cubey"
|
||||||
self.color = 0xe973ea
|
self.color = 0xe973ea
|
||||||
self.users = []
|
self.users = []
|
||||||
|
|
||||||
self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||||
print("Created socket object")
|
print("Created socket object")
|
||||||
|
|
||||||
self.ip = "0.0.0.0"
|
self.ip = "0.0.0.0"
|
||||||
self.port = 1337
|
self.port = 1337
|
||||||
self.address = (self.ip, self.port)
|
self.address = (self.ip, self.port)
|
||||||
self.sock.bind(self.address)
|
self.sock.bind(self.address)
|
||||||
print("Server started up on {}".format(self.address))
|
print("Server started up on {}".format(self.address))
|
||||||
|
|
||||||
def send(self, data, client):
|
def send(self, data, client):
|
||||||
data = json.dumps(data).encode()
|
data = json.dumps(data).encode()
|
||||||
self.sock.sendto(data, client)
|
self.sock.sendto(data, client)
|
||||||
|
|
||||||
def send_all(self, data):
|
def send_all(self, data):
|
||||||
for user in self.users:
|
for user in self.users:
|
||||||
self.send(data, user)
|
self.send(data, user)
|
||||||
|
|
||||||
def connection(self, address, connecting_client_name):
|
def connection(self, address, connecting_client_name):
|
||||||
print("New connection from {}".format(address))
|
print("New connection from {}".format(address))
|
||||||
a = {"header":"connection",
|
a = {"header":"connection",
|
||||||
"motd":self.motd,
|
"motd":self.motd,
|
||||||
"color":self.color,
|
"color":self.color,
|
||||||
"client name":connecting_client_name}
|
"client name":connecting_client_name}
|
||||||
print(">> " + connecting_client_name + " <<")
|
print(">> " + connecting_client_name + " <<")
|
||||||
self.users.append(address)
|
self.users.append(address)
|
||||||
self.send_all(a)
|
self.send_all(a)
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
data, addr = self.sock.recvfrom(65507)
|
data, addr = self.sock.recvfrom(65507)
|
||||||
a = json.loads(data)
|
a = json.loads(data)
|
||||||
header = a["header"]
|
header = a["header"]
|
||||||
client_name = a["client name"]
|
client_name = a["client name"]
|
||||||
|
|
||||||
if header == "connection":
|
if header == "connection":
|
||||||
self.connection(addr, client_name)
|
self.connection(addr, client_name)
|
||||||
elif header == "message":
|
elif header == "message":
|
||||||
message = a["message"]
|
message = a["message"]
|
||||||
print(client_name + " >> " + message)
|
print(client_name + " >> " + message)
|
||||||
response = {"header":"response",
|
response = {"header":"response",
|
||||||
"message":message,
|
"message":message,
|
||||||
"color":0xffffff,
|
"color":0xffffff,
|
||||||
"client name":client_name}
|
"client name":client_name}
|
||||||
if addr not in self.users:
|
if addr not in self.users:
|
||||||
self.users.append(addr)
|
self.users.append(addr)
|
||||||
self.send_all(response)
|
self.send_all(response)
|
||||||
except OSError:
|
except OSError:
|
||||||
return
|
return
|
||||||
|
|
||||||
class Commands:
|
class Commands:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.commands={"test":self.test,
|
self.commands={"test":self.test,
|
||||||
"stop":self.stop}
|
"stop":self.stop}
|
||||||
self.running = True
|
self.running = True
|
||||||
|
|
||||||
def test(self):
|
def test(self):
|
||||||
print("this is a test command")
|
print("this is a test command")
|
||||||
|
|
||||||
def stop(self):
|
def stop(self):
|
||||||
print("Stopping server...")
|
print("Stopping server...")
|
||||||
server.sock.close()
|
server.sock.close()
|
||||||
server_thread.join()
|
server_thread.join()
|
||||||
self.running = False
|
self.running = False
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
while self.running:
|
while self.running:
|
||||||
command = input()
|
command = input()
|
||||||
if command in self.commands:
|
if command in self.commands:
|
||||||
self.commands[command]()
|
self.commands[command]()
|
||||||
else:
|
else:
|
||||||
print("Unknown command")
|
print("Unknown command")
|
||||||
|
|
||||||
server = Server()
|
server = Server()
|
||||||
commands = Commands()
|
commands = Commands()
|
||||||
|
|
||||||
server_thread = threading.Thread(target = server.run)
|
server_thread = threading.Thread(target = server.run)
|
||||||
commands_thread = threading.Thread(target = commands.run)
|
commands_thread = threading.Thread(target = commands.run)
|
||||||
|
|
||||||
server_thread.start()
|
server_thread.start()
|
||||||
commands_thread.start()
|
commands_thread.start()
|
||||||
Reference in New Issue
Block a user