diff --git a/python/test/Client/Page/Login.py b/python/test/Client/Page/Login.py index 80429d4..a290dd4 100644 --- a/python/test/Client/Page/Login.py +++ b/python/test/Client/Page/Login.py @@ -5,10 +5,16 @@ import os import wx import threading import time -from Client.Process_session.Process_Client import ProcessClient +from Client.Transmission.Process_Client import ProcessClient from .Chat_main import ChatFrame +def open_chat_window(): + app = wx.App() + ChatFrame().Show() + app.MainLoop() + + class LoginFrame(wx.Frame, ProcessClient): def __init__(self): 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"] == '登录': match receive_content["data"]: case 0: - app = wx.App() - frame = ChatFrame() - frame.Show() - app.MainLoop() + multiprocessing.Process(target=open_chat_window).start() self.Close() case -1: wx.MessageBox('重复登录', '登录', wx.OK | wx.ICON_INFORMATION) diff --git a/python/test/Client/Socket_session/__init__.py b/python/test/Client/Socket_session/__init__.py deleted file mode 100644 index 6278850..0000000 --- a/python/test/Client/Socket_session/__init__.py +++ /dev/null @@ -1 +0,0 @@ -from .Session_server import * diff --git a/python/test/Client/Process_session/Process_Client.py b/python/test/Client/Transmission/Process_Client.py similarity index 96% rename from python/test/Client/Process_session/Process_Client.py rename to python/test/Client/Transmission/Process_Client.py index 9376b42..9145001 100644 --- a/python/test/Client/Process_session/Process_Client.py +++ b/python/test/Client/Transmission/Process_Client.py @@ -5,7 +5,7 @@ from multiprocessing.connection import Client class ProcessClient: def __init__(self): - self.Process_port = 12321 + self.Process_port = 8727 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) diff --git a/python/test/Client/Transmission/Process_Server.py b/python/test/Client/Transmission/Process_Server.py new file mode 100644 index 0000000..a2609da --- /dev/null +++ b/python/test/Client/Transmission/Process_Server.py @@ -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) diff --git a/python/test/Client/Socket_session/Session_server.py b/python/test/Client/Transmission/Session_server.py similarity index 95% rename from python/test/Client/Socket_session/Session_server.py rename to python/test/Client/Transmission/Session_server.py index 9dd02c9..1745161 100644 --- a/python/test/Client/Socket_session/Session_server.py +++ b/python/test/Client/Transmission/Session_server.py @@ -4,14 +4,14 @@ import time import socket import json import threading -from Client.Process_session.Process_Client import ProcessClient +from .Process_Client import ProcessClient class Session_server(ProcessClient): def __init__(self): ProcessClient.__init__(self) self.socker_ip = "127.0.0.1" - self.socker_port = 8000 + self.socker_port = 7868 self.server_socket = None self.server_status = False # 服务器状态 self.link_server_Thread = threading.Thread(target=self.link_server) @@ -70,6 +70,5 @@ class Session_server(ProcessClient): match data['function']: case 'server_status': self.server_status = data['content'] - print(self.server_status) case 'send_server': self.send_server(data['content']['genre'], data['content']['target'], data['content']['content']) diff --git a/python/test/Client/Process_session/__init__.py b/python/test/Client/Transmission/__init__.py similarity index 67% rename from python/test/Client/Process_session/__init__.py rename to python/test/Client/Transmission/__init__.py index 5690469..fb5046f 100644 --- a/python/test/Client/Process_session/__init__.py +++ b/python/test/Client/Transmission/__init__.py @@ -1,2 +1,3 @@ from .Process_Server import * from .Process_Client import * +from .Session_server import * \ No newline at end of file diff --git a/python/test/Client/__init__.py b/python/test/Client/__init__.py index 706cb7a..e727993 100644 --- a/python/test/Client/__init__.py +++ b/python/test/Client/__init__.py @@ -1,6 +1,5 @@ from .Page import * -from .Socket_session import * -from .Process_session import * +from .Transmission import * class start_all: @@ -10,6 +9,6 @@ class start_all: Session_server() app = wx.App() - frame = LoginFrame() - frame.Show() + LoginFrame().Show() app.MainLoop() + diff --git a/python/test/Server/Session_client.py b/python/test/Server/Session_client.py deleted file mode 100644 index daae258..0000000 --- a/python/test/Server/Session_client.py +++ /dev/null @@ -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 diff --git a/python/test/Server/Database_formula.py b/python/test/Server/Transmission/Database_formula.py similarity index 66% rename from python/test/Server/Database_formula.py rename to python/test/Server/Transmission/Database_formula.py index f35b7b1..d4e4205 100644 --- a/python/test/Server/Database_formula.py +++ b/python/test/Server/Transmission/Database_formula.py @@ -1,10 +1,14 @@ +import os + import pymssql import threading import time +from .Process_Client import * -class database: +class database(ProcessClient): def __init__(self): + ProcessClient.__init__(self) self.database_server = 'localhost' self.database_user = 'Chat' self.database_password = '123456' @@ -14,6 +18,10 @@ class database: self.database_conn = 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.start() @@ -76,3 +84,26 @@ class database: except pymssql as a: self.database_state = False 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) diff --git a/python/test/Server/Transmission/Process_Client.py b/python/test/Server/Transmission/Process_Client.py new file mode 100644 index 0000000..4938987 --- /dev/null +++ b/python/test/Server/Transmission/Process_Client.py @@ -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 diff --git a/python/test/Client/Process_session/Process_Server.py b/python/test/Server/Transmission/Process_Server.py similarity index 77% rename from python/test/Client/Process_session/Process_Server.py rename to python/test/Server/Transmission/Process_Server.py index 77bb470..8ff2369 100644 --- a/python/test/Client/Process_session/Process_Server.py +++ b/python/test/Server/Transmission/Process_Server.py @@ -6,12 +6,15 @@ import threading class ProcessServer: def __init__(self): - self.Process_port = 12321 - 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() + try: + self.Process_port = 3244 + 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: diff --git a/python/test/Server/Transmission/Session_client.py b/python/test/Server/Transmission/Session_client.py new file mode 100644 index 0000000..d6f3587 --- /dev/null +++ b/python/test/Server/Transmission/Session_client.py @@ -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": "在线"}) diff --git a/python/test/Server/Transmission/__init__.py b/python/test/Server/Transmission/__init__.py new file mode 100644 index 0000000..1e3f2fb --- /dev/null +++ b/python/test/Server/Transmission/__init__.py @@ -0,0 +1,4 @@ +from .Process_Server import * +from .Process_Client import * +from .Session_client import * +from .Database_formula import * diff --git a/python/test/Server/__init__.py b/python/test/Server/__init__.py index 16edef0..21e5173 100644 --- a/python/test/Server/__init__.py +++ b/python/test/Server/__init__.py @@ -1,24 +1,11 @@ -from .Session_client import * -from .Database_formula import * +from .Transmission import * -class Server_main(link_client, database): +class Server_main: def __init__(self): - link_client.__init__(self) - database.__init__(self) + ProcessServer() + 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) diff --git a/python/test/StartClient.py b/python/test/StartClient.py index 619bc72..18a9d80 100644 --- a/python/test/StartClient.py +++ b/python/test/StartClient.py @@ -1,4 +1,3 @@ -import wx from Client import start_all if __name__ == '__main__': diff --git a/python/test/StartServer.py b/python/test/StartServer.py index 6ba1bb0..8f05cdb 100644 --- a/python/test/StartServer.py +++ b/python/test/StartServer.py @@ -1,4 +1,4 @@ from Server import Server_main if __name__ == '__main__': - server = Server_main() + Server_main() diff --git a/python/test/start_all.py b/python/test/start_all.py new file mode 100644 index 0000000..a912f00 --- /dev/null +++ b/python/test/start_all.py @@ -0,0 +1,6 @@ +from Client import start_all +from Server import Server_main + +if __name__ == '__main__': + Server_main() + start_all() \ No newline at end of file diff --git a/python/test/test.py b/python/test/test.py index 8e169a5..f4e6289 100644 --- a/python/test/test.py +++ b/python/test/test.py @@ -6,26 +6,32 @@ import threading class ProcessServer: def __init__(self): - self.Process_port = 12321 - 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() + try: + self.Process_port = 3244 + 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() - 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,)) - client_process_recv.start() - - def Process_client_recv(self, client_process_recv): + def Process_client_recv(self, client_Thread_recv): while True: - data_json = client_process_recv.recv() + data_json = client_Thread_recv.recv() 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): connect = self.Process_client_link_dick[target] @@ -33,5 +39,16 @@ class ProcessServer: data_json = json.dumps(data) connect.send(data_json) - def Process_client_pick(self): - pass + 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) + + +if __name__ == '__main__': + ProcessServer() \ No newline at end of file diff --git a/python/test/test2.py b/python/test/test2.py index c9123ab..db1f26a 100644 --- a/python/test/test2.py +++ b/python/test/test2.py @@ -5,11 +5,10 @@ from multiprocessing.connection import Client class ProcessClient: def __init__(self): - self.Process_port = 12321 + 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) - print(self.Process_client_Client) Process_client_recv.start() def Process_client_send(self, target, function, content): @@ -22,17 +21,13 @@ class ProcessClient: try: data_json = self.Process_client_Client.recv() data = json.loads(data_json) - print(data) + self.Process_client_pick(data) except EOFError: print("连接已关闭") break - def Process_client_pick(self): + def Process_client_pick(self, data): pass - if __name__ == '__main__': - xx = ProcessClient() - while True: - a = input("请输入") - xx.Process_client_send(a, a, a) + ProcessClient() \ No newline at end of file diff --git a/python/test/test_plan.py b/python/test/test_plan.py deleted file mode 100644 index 149f2f8..0000000 --- a/python/test/test_plan.py +++ /dev/null @@ -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() -