服务端json无法压缩套接字

This commit is contained in:
lsy2246 2024-04-17 16:45:36 +08:00
parent 783bc58482
commit 8c212e3803
20 changed files with 277 additions and 143 deletions

View File

@ -5,10 +5,16 @@ import os
import wx import wx
import threading import threading
import time import time
from Client.Process_session.Process_Client import ProcessClient from Client.Transmission.Process_Client import ProcessClient
from .Chat_main import ChatFrame from .Chat_main import ChatFrame
def open_chat_window():
app = wx.App()
ChatFrame().Show()
app.MainLoop()
class LoginFrame(wx.Frame, ProcessClient): class LoginFrame(wx.Frame, ProcessClient):
def __init__(self): def __init__(self):
wx.Frame.__init__(self, None, id=-1, title='登录', pos=wx.DefaultPosition, size=(380, 300)) wx.Frame.__init__(self, None, id=-1, title='登录', pos=wx.DefaultPosition, size=(380, 300))
@ -103,10 +109,7 @@ class LoginFrame(wx.Frame, ProcessClient):
if receive_content["genre"] == '登录': if receive_content["genre"] == '登录':
match receive_content["data"]: match receive_content["data"]:
case 0: case 0:
app = wx.App() multiprocessing.Process(target=open_chat_window).start()
frame = ChatFrame()
frame.Show()
app.MainLoop()
self.Close() self.Close()
case -1: case -1:
wx.MessageBox('重复登录', '登录', wx.OK | wx.ICON_INFORMATION) wx.MessageBox('重复登录', '登录', wx.OK | wx.ICON_INFORMATION)

View File

@ -1 +0,0 @@
from .Session_server import *

View File

@ -5,7 +5,7 @@ from multiprocessing.connection import Client
class ProcessClient: class ProcessClient:
def __init__(self): def __init__(self):
self.Process_port = 12321 self.Process_port = 8727
self.Process_server = 'localhost' self.Process_server = 'localhost'
self.Process_client_Client = Client((self.Process_server, self.Process_port)) self.Process_client_Client = Client((self.Process_server, self.Process_port))
Process_client_recv = threading.Thread(target=self.Process_client_recv) Process_client_recv = threading.Thread(target=self.Process_client_recv)

View File

@ -0,0 +1,50 @@
import json
import multiprocessing
from multiprocessing.connection import Listener
import threading
class ProcessServer:
def __init__(self):
try:
self.Process_port = 8727
self.Process_server = 'localhost'
self.Process_server_listener = Listener((self.Process_server, self.Process_port))
self.Process_client_link_dick = {}
Process_client_link_Thread = threading.Thread(target=self.Process_client_link)
Process_client_link_Thread.start()
except:
print("进程通信端口绑定失败")
def Process_client_link(self):
while True:
client_connect = self.Process_server_listener.accept()
client_Thread_recv = threading.Thread(target=self.Process_client_recv, args=(client_connect,))
client_Thread_recv.start()
def Process_client_recv(self, client_Thread_recv):
while True:
data_json = client_Thread_recv.recv()
data = json.loads(data_json)
if data['target'] == 'Server':
if data['function'] == 'Name':
self.Process_client_link_dick[data['content']] = client_Thread_recv
else:
self.Process_client_pick(data)
def Process_client_send(self, target, function, content):
connect = self.Process_client_link_dick[target]
data = {"target": target, "function": function, "content": content}
data_json = json.dumps(data)
connect.send(data_json)
def Process_client_pick(self, data):
if data['target'] == 'ALL':
for value in self.Process_client_link_dick.values():
data_json = json.dumps(data)
value.send(data_json)
else:
if data['target'] in self.Process_client_link_dick.keys():
data_json = json.dumps(data)
self.Process_client_link_dick[data['target']].send(data_json)

View File

@ -4,14 +4,14 @@ import time
import socket import socket
import json import json
import threading import threading
from Client.Process_session.Process_Client import ProcessClient from .Process_Client import ProcessClient
class Session_server(ProcessClient): class Session_server(ProcessClient):
def __init__(self): def __init__(self):
ProcessClient.__init__(self) ProcessClient.__init__(self)
self.socker_ip = "127.0.0.1" self.socker_ip = "127.0.0.1"
self.socker_port = 8000 self.socker_port = 7868
self.server_socket = None self.server_socket = None
self.server_status = False # 服务器状态 self.server_status = False # 服务器状态
self.link_server_Thread = threading.Thread(target=self.link_server) self.link_server_Thread = threading.Thread(target=self.link_server)
@ -70,6 +70,5 @@ class Session_server(ProcessClient):
match data['function']: match data['function']:
case 'server_status': case 'server_status':
self.server_status = data['content'] self.server_status = data['content']
print(self.server_status)
case 'send_server': case 'send_server':
self.send_server(data['content']['genre'], data['content']['target'], data['content']['content']) self.send_server(data['content']['genre'], data['content']['target'], data['content']['content'])

View File

@ -1,2 +1,3 @@
from .Process_Server import * from .Process_Server import *
from .Process_Client import * from .Process_Client import *
from .Session_server import *

View File

@ -1,6 +1,5 @@
from .Page import * from .Page import *
from .Socket_session import * from .Transmission import *
from .Process_session import *
class start_all: class start_all:
@ -10,6 +9,6 @@ class start_all:
Session_server() Session_server()
app = wx.App() app = wx.App()
frame = LoginFrame() LoginFrame().Show()
frame.Show()
app.MainLoop() app.MainLoop()

View File

@ -1,68 +0,0 @@
import time
import socket
import json
import threading
class link_client:
def __init__(self):
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()
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):
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()
def pick_data(self, data):
pass
def recv_client(self, client_socket):
state = True
while state:
try:
data_json = client_socket.recv(1024).decode('utf-8')
data = json.loads(data_json)
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"] == '注册':
content = self.pick_data(data)
self.send_client(client_socket, data["genre"], '客户端', content)
else:
self.pick_data(data)
except:
try:
client_socket.close()
finally:
state = False
self.alter_state(client_socket, "离线")
def alter_state(self, client_socket, state):
pass

View File

@ -1,10 +1,14 @@
import os
import pymssql import pymssql
import threading import threading
import time import time
from .Process_Client import *
class database: class database(ProcessClient):
def __init__(self): def __init__(self):
ProcessClient.__init__(self)
self.database_server = 'localhost' self.database_server = 'localhost'
self.database_user = 'Chat' self.database_user = 'Chat'
self.database_password = '123456' self.database_password = '123456'
@ -14,6 +18,10 @@ class database:
self.database_conn = None self.database_conn = None
self.database_cursor = None self.database_cursor = None
current_file_path = __file__
current_file_name = os.path.basename(current_file_path).split('.')[0]
self.Process_client_send("Server", "Name", current_file_name)
self.link_database_thread = threading.Thread(target=self.link_database) self.link_database_thread = threading.Thread(target=self.link_database)
self.link_database_thread.start() self.link_database_thread.start()
@ -76,3 +84,26 @@ class database:
except pymssql as a: except pymssql as a:
self.database_state = False self.database_state = False
print(a) print(a)
def Process_client_pick(self, data):
if data['target'] in ['ALL', 'Database_formula']:
match data['function']:
case 'check_account_state':
client_socket = data['content']['client_socket']
account = data['content']['account']
password = data['content']['password']
status = self.check_account_state(account, password)
content = {"account": account, "status": status}
content = {"client_socket": client_socket, "genre": "客户端", "target": "登录", "content": content}
self.Process_client_send('Session_client', 'send_client', content)
case 'sign_account':
client_socket = data['content']['client_socket']
account = data['content']['account']
password = data['content']['password']
info = self.sign_account(account, password)
content = {"client_socket": client_socket, "genre": "客户端", "target": "注册", "content": info}
self.Process_client_send('Session_client', 'send_client', content)

View File

@ -0,0 +1,30 @@
import json
import threading
from multiprocessing.connection import Client
class ProcessClient:
def __init__(self):
self.Process_port = 3244
self.Process_server = 'localhost'
self.Process_client_Client = Client((self.Process_server, self.Process_port))
Process_client_recv = threading.Thread(target=self.Process_client_recv)
Process_client_recv.start()
def Process_client_send(self, target, function, content):
data = {"target": target, "function": function, "content": content}
data_json = json.dumps(data)
self.Process_client_Client.send(data_json)
def Process_client_recv(self):
while True:
try:
data_json = self.Process_client_Client.recv()
data = json.loads(data_json)
self.Process_client_pick(data)
except EOFError:
print("连接已关闭")
break
def Process_client_pick(self, data):
pass

View File

@ -6,12 +6,15 @@ import threading
class ProcessServer: class ProcessServer:
def __init__(self): def __init__(self):
self.Process_port = 12321 try:
self.Process_server = 'localhost' self.Process_port = 3244
self.Process_server_listener = Listener((self.Process_server, self.Process_port)) self.Process_server = 'localhost'
self.Process_client_link_dick = {} self.Process_server_listener = Listener((self.Process_server, self.Process_port))
Process_client_link_Thread = threading.Thread(target=self.Process_client_link) self.Process_client_link_dick = {}
Process_client_link_Thread.start() Process_client_link_Thread = threading.Thread(target=self.Process_client_link)
Process_client_link_Thread.start()
except:
print("进程通信端口绑定失败")
def Process_client_link(self): def Process_client_link(self):
while True: while True:

View File

@ -0,0 +1,88 @@
import os
import time
import socket
import json
import threading
from .Process_Client import *
class link_client(ProcessClient):
def __init__(self):
ProcessClient.__init__(self)
self.server_host = "127.0.0.1"
self.server_port = 7868
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 = {}
current_file_path = __file__
current_file_name = os.path.basename(current_file_path).split('.')[0]
self.Process_client_send("Server", "Name", current_file_name)
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()
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):
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()
def pick_data(self, client_socket, data):
match data['genre']:
case '登录':
content = {'client_socket': client_socket, 'account': data['data']['account'],
'password': data['data']['password']}
self.Process_client_send("Database_formula", "check_account_state", content)
case '注册':
content = {'client_socket': client_socket, 'account': data['data']['account'],
'password': data['data']['password']}
self.Process_client_send("Database_formula", "sign_account", content)
def recv_client(self, client_socket):
state = True
while state:
try:
data_json = client_socket.recv(1024).decode('utf-8')
data = json.loads(data_json)
self.pick_data(client_socket, data)
except:
try:
client_socket.close()
finally:
state = False
for key, value in self.client_socket_dict.items():
if value == client_socket:
del self.client_socket_dict[key]
self.Process_client_send("Database_formula", "alter_state_database",
{"Id": key, "sate": "在线"})
break
def Process_client_pick(self, data):
if data['target'] in ['ALL', 'Session_client']:
match data['function']:
case 'send_client':
client_socket = data['content']['client_socket']
genre = data['content']['genre']
target = data['content']['target']
content = data['content']['content']
self.send_client(client_socket, genre, target, content)
if target == '登录':
if data['content']['status'] == 0:
self.client_socket_dict[data['content']['account']] = data['data']['client_socket']
self.Process_client_send("Database_formula", "alter_state_database",
{"Id": data['content']['account'],
"sate": "在线"})

View File

@ -0,0 +1,4 @@
from .Process_Server import *
from .Process_Client import *
from .Session_client import *
from .Database_formula import *

View File

@ -1,24 +1,11 @@
from .Session_client import * from .Transmission import *
from .Database_formula import *
class Server_main(link_client, database): class Server_main:
def __init__(self): def __init__(self):
link_client.__init__(self) ProcessServer()
database.__init__(self) database()
link_client()
def pick_data(self, data):
account_state = None
if data['genre'] == '登录':
account_state = self.check_account_state(data['data']['account'], data['data']['password'])
return account_state
elif data['genre'] == '注册':
account_data = self.sign_account(data['data']['account'], data['data']['password'])
return account_data
def alter_state(self, client_socket, state):
for key, value in self.client_socket_dict.items():
if value == client_socket:
del self.client_socket_dict[key]
break
self.alter_state_database(client_socket, state)

View File

@ -1,4 +1,3 @@
import wx
from Client import start_all from Client import start_all
if __name__ == '__main__': if __name__ == '__main__':

View File

@ -1,4 +1,4 @@
from Server import Server_main from Server import Server_main
if __name__ == '__main__': if __name__ == '__main__':
server = Server_main() Server_main()

6
python/test/start_all.py Normal file
View File

@ -0,0 +1,6 @@
from Client import start_all
from Server import Server_main
if __name__ == '__main__':
Server_main()
start_all()

View File

@ -6,26 +6,32 @@ import threading
class ProcessServer: class ProcessServer:
def __init__(self): def __init__(self):
self.Process_port = 12321 try:
self.Process_server = 'localhost' self.Process_port = 3244
self.Process_server_listener = Listener((self.Process_server, self.Process_port)) self.Process_server = 'localhost'
self.Process_client_link_dick = {} self.Process_server_listener = Listener((self.Process_server, self.Process_port))
Process_client_link_Thread = threading.Thread(target=self.Process_client_link) self.Process_client_link_dick = {}
Process_client_link_Thread.start() Process_client_link_Thread = threading.Thread(target=self.Process_client_link)
Process_client_link_Thread.start()
except:
print("进程通信端口绑定失败")
def Process_client_link(self): def Process_client_link(self):
while True: while True:
client_connect = self.Process_server_listener.accept() client_connect = self.Process_server_listener.accept()
print(type(client_connect)) client_Thread_recv = threading.Thread(target=self.Process_client_recv, args=(client_connect,))
client_Thread_recv.start()
client_process_recv = threading.Thread(target=self.Process_client_recv, args=(client_connect,)) def Process_client_recv(self, client_Thread_recv):
client_process_recv.start()
def Process_client_recv(self, client_process_recv):
while True: while True:
data_json = client_process_recv.recv() data_json = client_Thread_recv.recv()
data = json.loads(data_json) data = json.loads(data_json)
print(data) if data['target'] == 'Server':
if data['function'] == 'Name':
self.Process_client_link_dick[data['content']] = client_Thread_recv
else:
self.Process_client_pick(data)
def Process_client_send(self, target, function, content): def Process_client_send(self, target, function, content):
connect = self.Process_client_link_dick[target] connect = self.Process_client_link_dick[target]
@ -33,5 +39,16 @@ class ProcessServer:
data_json = json.dumps(data) data_json = json.dumps(data)
connect.send(data_json) connect.send(data_json)
def Process_client_pick(self): def Process_client_pick(self, data):
pass if data['target'] == 'ALL':
for value in self.Process_client_link_dick.values():
data_json = json.dumps(data)
value.send(data_json)
else:
if data['target'] in self.Process_client_link_dick.keys():
data_json = json.dumps(data)
self.Process_client_link_dick[data['target']].send(data_json)
if __name__ == '__main__':
ProcessServer()

View File

@ -5,11 +5,10 @@ from multiprocessing.connection import Client
class ProcessClient: class ProcessClient:
def __init__(self): def __init__(self):
self.Process_port = 12321 self.Process_port = 3244
self.Process_server = 'localhost' self.Process_server = 'localhost'
self.Process_client_Client = Client((self.Process_server, self.Process_port)) self.Process_client_Client = Client((self.Process_server, self.Process_port))
Process_client_recv = threading.Thread(target=self.Process_client_recv) Process_client_recv = threading.Thread(target=self.Process_client_recv)
print(self.Process_client_Client)
Process_client_recv.start() Process_client_recv.start()
def Process_client_send(self, target, function, content): def Process_client_send(self, target, function, content):
@ -22,17 +21,13 @@ class ProcessClient:
try: try:
data_json = self.Process_client_Client.recv() data_json = self.Process_client_Client.recv()
data = json.loads(data_json) data = json.loads(data_json)
print(data) self.Process_client_pick(data)
except EOFError: except EOFError:
print("连接已关闭") print("连接已关闭")
break break
def Process_client_pick(self): def Process_client_pick(self, data):
pass pass
if __name__ == '__main__': if __name__ == '__main__':
xx = ProcessClient() ProcessClient()
while True:
a = input("请输入")
xx.Process_client_send(a, a, a)

View File

@ -1,9 +0,0 @@
import wx
from Client.Page.Chat_main import ChatFrame
if __name__ == '__main__':
app = wx.App()
frame = ChatFrame("lsy")
frame.Show()
app.MainLoop()