Update 'client.py'
This commit is contained in:
142
client.py
142
client.py
@@ -1,72 +1,72 @@
|
|||||||
import socket, sys, datetime, threading, json
|
import socket, sys, datetime, threading, json
|
||||||
|
|
||||||
class Client:
|
class Client:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
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.client_name = "CLI"
|
self.client_name = "CLI"
|
||||||
|
|
||||||
self.server_ip = "cubes.link"
|
self.server_ip = "cubes.link"
|
||||||
self.server_port = 1337
|
self.server_port = 1337
|
||||||
self.server_address = (self.server_ip, self.server_port)
|
self.server_address = (self.server_ip, self.server_port)
|
||||||
|
|
||||||
a = {"header":"connection",
|
a = {"header":"connection",
|
||||||
"client name":self.client_name}
|
"client name":self.client_name}
|
||||||
self.send(a, self.server_address)
|
self.send(a, self.server_address)
|
||||||
|
|
||||||
def send(self, data, addr):
|
def send(self, data, addr):
|
||||||
data = json.dumps(data).encode()
|
data = json.dumps(data).encode()
|
||||||
self.sock.sendto(data, addr)
|
self.sock.sendto(data, addr)
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
while True:
|
while True:
|
||||||
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"]
|
||||||
|
|
||||||
if header == "response":
|
if header == "response":
|
||||||
message = a["message"]
|
message = a["message"]
|
||||||
client_name = a["client name"]
|
client_name = a["client name"]
|
||||||
print(client_name + " >> " + message)
|
print(client_name + " >> " + message)
|
||||||
|
|
||||||
elif header == "connection":
|
elif header == "connection":
|
||||||
motd = a["motd"]
|
motd = a["motd"]
|
||||||
client_name = a["client name"]
|
client_name = a["client name"]
|
||||||
print(">> " + client_name + " <<")
|
print(">> " + client_name + " <<")
|
||||||
|
|
||||||
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, params):
|
def test(self, params):
|
||||||
print("this is a test command")
|
print("this is a test command")
|
||||||
|
|
||||||
def stop(self, params):
|
def stop(self, params):
|
||||||
print("this doesn't do anything rn")
|
print("this doesn't do anything rn")
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
while self.running:
|
while self.running:
|
||||||
text = input()
|
text = input()
|
||||||
if text[0] == "/":
|
if text[0] == "/":
|
||||||
parts = text.split(" ")
|
parts = text.split(" ")
|
||||||
command = parts[0].strip("/")
|
command = parts[0].strip("/")
|
||||||
params = parts.pop(0)
|
params = parts.pop(0)
|
||||||
if command in self.commands:
|
if command in self.commands:
|
||||||
self.commands[command](params)
|
self.commands[command](params)
|
||||||
else:
|
else:
|
||||||
a = {"header":"message",
|
a = {"header":"message",
|
||||||
"message":text,
|
"message":text,
|
||||||
"client name":client.client_name}
|
"client name":client.client_name}
|
||||||
client.send(a, client.server_address)
|
client.send(a, client.server_address)
|
||||||
|
|
||||||
client = Client()
|
client = Client()
|
||||||
commands = Commands()
|
commands = Commands()
|
||||||
|
|
||||||
client_thread = threading.Thread(target = client.run)
|
client_thread = threading.Thread(target = client.run)
|
||||||
commands_thread = threading.Thread(target = commands.run)
|
commands_thread = threading.Thread(target = commands.run)
|
||||||
|
|
||||||
client_thread.start()
|
client_thread.start()
|
||||||
commands_thread.start()
|
commands_thread.start()
|
||||||
Reference in New Issue
Block a user