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