2024-04-12 00:56:58 +08:00
|
|
|
import time
|
|
|
|
import socket
|
|
|
|
import json
|
|
|
|
import threading
|
|
|
|
|
|
|
|
|
|
|
|
class link_client:
|
2024-04-13 01:40:25 +08:00
|
|
|
def __init__(self):
|
2024-04-12 00:56:58 +08:00
|
|
|
self.server_host = "127.0.0.1"
|
|
|
|
self.server_port = 8000
|
|
|
|
self.server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
|
|
self.server_socket.bind((self.server_host, self.server_port))
|
|
|
|
self.server_socket.listen()
|
|
|
|
self.client_socket_dict = {}
|
|
|
|
|
|
|
|
self.link_client_Thread = threading.Thread(target=self.link_client)
|
|
|
|
self.link_client_Thread.start()
|
|
|
|
|
|
|
|
def link_client(self):
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
client_socket, client_address = self.server_socket.accept()
|
2024-04-13 01:40:25 +08:00
|
|
|
recv_client_Thread = threading.Thread(target=self.recv_client(client_socket), daemon=True)
|
|
|
|
recv_client_Thread.start()
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
|
|
|
def send_client(self, client_socket, genre, target, content):
|
2024-04-13 13:52:36 +08:00
|
|
|
try:
|
|
|
|
data = {"genre": genre, "target": target, "data": content,
|
|
|
|
"datetime": time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())}
|
|
|
|
data_json = json.dumps(data)
|
|
|
|
client_socket.send(data_json.encode("utf-8"))
|
|
|
|
except:
|
|
|
|
client_socket.close()
|
2024-04-13 01:40:25 +08:00
|
|
|
|
|
|
|
def pick_data(self, data):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def recv_client(self, client_socket):
|
2024-04-13 13:52:36 +08:00
|
|
|
state = True
|
|
|
|
while state:
|
2024-04-13 01:40:25 +08:00
|
|
|
try:
|
|
|
|
data_json = client_socket.recv(1024).decode('utf-8')
|
2024-04-12 00:56:58 +08:00
|
|
|
data = json.loads(data_json)
|
2024-04-13 13:52:36 +08:00
|
|
|
if data["genre"] == '登录':
|
|
|
|
content = self.pick_data(data)
|
|
|
|
if data['data']['account'] not in self.client_socket_dict:
|
|
|
|
if content == 0:
|
|
|
|
self.client_socket_dict[data['data']['account']] = client_socket
|
|
|
|
self.alter_state(data['data']['account'], "在线")
|
|
|
|
self.send_client(client_socket, data["genre"], '客户端', content)
|
|
|
|
else:
|
|
|
|
self.send_client(client_socket, data["genre"], '客户端', -1)
|
|
|
|
elif data["genre"] == '注册':
|
2024-04-13 01:40:25 +08:00
|
|
|
content = self.pick_data(data)
|
2024-04-13 13:52:36 +08:00
|
|
|
self.send_client(client_socket, data["genre"], '客户端', content)
|
2024-04-13 01:40:25 +08:00
|
|
|
else:
|
|
|
|
self.pick_data(data)
|
2024-04-12 00:56:58 +08:00
|
|
|
except:
|
2024-04-13 13:52:36 +08:00
|
|
|
try:
|
|
|
|
client_socket.close()
|
|
|
|
finally:
|
|
|
|
state = False
|
|
|
|
self.alter_state(client_socket, "离线")
|
|
|
|
|
|
|
|
def alter_state(self, client_socket, state):
|
|
|
|
pass
|