From c8e0af64ac581c09150c51e2e932b3029d795238 Mon Sep 17 00:00:00 2001 From: lsy2246 Date: Sat, 13 Apr 2024 13:52:36 +0800 Subject: [PATCH] =?UTF-8?q?=E9=9C=80=E8=A6=81=E6=B7=BB=E5=8A=A0=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E5=BA=93=E6=9C=AA=E8=BF=9E=E6=8E=A5=E6=83=85=E5=86=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- python/test/Cilent/Login.py | 2 - python/test/Cilent/Session_server.py | 18 +++-- python/test/Cilent/__init__.py | 17 ++++- .../Cilent/__pycache__/Login.cpython-312.pyc | Bin 14191 -> 14191 bytes .../Session_server.cpython-312.pyc | Bin 3636 -> 3887 bytes .../__pycache__/__init__.cpython-312.pyc | Bin 2498 -> 3980 bytes python/test/Server/Database_formula.py | 29 ++++++-- python/test/Server/Session_client.py | 45 ++++++++----- python/test/Server/__init__.py | 12 +++- python/test/test.py | 62 +++++------------- 10 files changed, 106 insertions(+), 79 deletions(-) diff --git a/python/test/Cilent/Login.py b/python/test/Cilent/Login.py index 8fe6ac5..b627039 100644 --- a/python/test/Cilent/Login.py +++ b/python/test/Cilent/Login.py @@ -54,7 +54,6 @@ class LoginFrame(wx.Frame): self.SetSizer(self.sizer) - def update_status_bar(self, event): if self.server_status: self.status_bar.SetStatusText("服务器\t已连接", 0) @@ -138,7 +137,6 @@ class LoginFrame(wx.Frame): self.SetSizer(LoginPanel_main_box) - class RegisterPanel(wx.Panel): def __init__(self, parent): super().__init__(parent) diff --git a/python/test/Cilent/Session_server.py b/python/test/Cilent/Session_server.py index 0152f18..29cfc39 100644 --- a/python/test/Cilent/Session_server.py +++ b/python/test/Cilent/Session_server.py @@ -26,20 +26,25 @@ class Session_server: self.server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.server_socket.connect((self.socker_ip, self.socker_port)) self.server_status = True - if not self.link_server_Thread.is_alive(): - print("Server") + if not self.receive_server_Thread.is_alive(): self.receive_server_Thread.start() except Exception as a: self.server_status = False - print("连接错误:"+str(a)) + print("连接错误:" + str(a)) + + def login_page_receive(self, receive_content): + pass def receive_server(self): while self.server_status: try: - receive_content_json = self.server_socket.recv(10240).decode('utf-8') # 追加接收到的数据 + receive_content_json = self.server_socket.recv(1024).decode('utf-8') receive_content = json.loads(receive_content_json) + if receive_content["genre"] in ['注册', '登录']: + self.login_page_receive(receive_content) + print(receive_content) except Exception as a: - print("接收错误:"+str(a)) + print("接收错误:" + str(a)) self.server_status = False def send_server(self, genre, target, content): @@ -50,6 +55,5 @@ class Session_server: data_json = json.dumps(data) self.server_socket.send(data_json.encode("utf-8")) except Exception as a: - print("发送错误:"+str(a)) + print("发送错误:" + str(a)) self.server_status = False - diff --git a/python/test/Cilent/__init__.py b/python/test/Cilent/__init__.py index ebd5db5..9d93afb 100644 --- a/python/test/Cilent/__init__.py +++ b/python/test/Cilent/__init__.py @@ -22,9 +22,22 @@ class Client_main(LoginFrame, Session_server): def send_register_button(self, event): if self.server_status: - account = self.login_panel.RegisterPanel_account_text.GetValue().strip() - password = self.login_panel.RegisterPanel_password_text.GetValue().strip() + account = self.register_panel.RegisterPanel_account_text.GetValue().strip() + password = self.register_panel.RegisterPanel_password_text.GetValue().strip() content = {'account': account, 'password': password} target = "服务器" genre = "注册" self.send_server(genre=genre, target=target, content=content) + + def login_page_receive(self, receive_content): + if receive_content["genre"] == '登录': + if receive_content["data"] == 0: + wx.MessageBox('登录成功', '登录', wx.OK | wx.ICON_INFORMATION) + elif receive_content["data"] == -1: + wx.MessageBox('重复登录', '登录', wx.OK | wx.ICON_INFORMATION) + elif receive_content["data"] == 1: + wx.MessageBox('密码错误', '登录', wx.OK | wx.ICON_INFORMATION) + elif receive_content["data"] == 2: + wx.MessageBox('未找到该账号', '登录', wx.OK | wx.ICON_INFORMATION) + elif receive_content["genre"] == '注册': + wx.MessageBox(f'注册成功\n网名 : {receive_content["data"]['NetName']} \n账号 : {receive_content["data"]['Id']} \n密码 : {receive_content["data"]['Password']}', '注册', wx.OK | wx.ICON_INFORMATION) \ No newline at end of file diff --git a/python/test/Cilent/__pycache__/Login.cpython-312.pyc b/python/test/Cilent/__pycache__/Login.cpython-312.pyc index 19ef947ba09bb3b373afaa14c0f4d40f3214e963..2e1e5c03f455e77ae5c8ae14db0b5c6122efc3f1 100644 GIT binary patch delta 104 zcmaE#_dbvJG%qg~0}wpnluAq4$Q!}WXt_C!U!IB4adVsC0wzZ9&FUgAco`!$3n*=6 zW(?naO!YSpWAEleMjgzIJ(Io7nix|ipEFZn%$oe(EL*gOiP30+&~%B35?>iW>>^vB F(Ezm*(40wzYE&FUgAco`!%3n*=6 zW{lW;O!YSpWB=wuMjgzIeUrV-ni$h2pEFZn%$fY&EL*gWiP30+&~%B35?>iW>>^vB F(E!SjAZY*q diff --git a/python/test/Cilent/__pycache__/Session_server.cpython-312.pyc b/python/test/Cilent/__pycache__/Session_server.cpython-312.pyc index dd39a30790721d847266a16213a9191807996e1a..f5bbde4f4c84e6d6ae6139ad4be81c321d10ae73 100644 GIT binary patch delta 1188 zcmZXUO>7%Q6vt=0Uhl5swKwr5tYgQqox~ewn-APXB%w{Q;}$_hkf5g?uq=BMxhi&; z-BhFmLZymRRk<1vRH7{$5P?(*C`cdyLbM-9|rI|42@j)qX(FsRD@bDK>e@70<#3NyBmaN*CV&!G`sO@?2}#W z%!xaY>>gVX32%RQb^GE+ciz5ydu?qpA>lvSXZl*new{BG+1%;k86ElPQaJi*UY|FM z$(r=d7TuK&6i82 zUNQ}u8;Kx4J#0H&o-3BJ^SL=ayBBvh`l4AWFRnU^P9GR7a!sS z!u%pBlDxl!U*ZD{Yx?n7J_=p(**U$0^n_!^OK)tErl;PFU)Mtxj3$)K})Fnz7a6mU{fBr#ICT8&dC9EdKGphXWgfsm)lr;;Z>% zA3S0CwTKLj@!?nJFiqn>2mK39dBWk;Yb1@Z59Rc4T5z-^f4L+RKm>U|K9$D{lV;DjS zS`}?b-C4+)xjIj%o5h!P)Q^8{2}1?{-4cKYFy{$RHoFm_2~mOs*Bq*S3f$gUp%JPl zP`&Iaqg*0g%X$vOB);NV_U!*++%G;3d3;hFZnhLjRC|J=m!h45&L$$SxC=T>Mu^}% zXgCD+1=#5DvZIX%WW|Qd&1!cQfz@^sX-7RTs?|z!Rqn)A{Kw%_DarOO)h1WlK<`3Wt-y@8vs{;NIXJ^O}EHF zXV<+)+z8CXjR0$7qrlyT8xaI&!PK^mc&kozk}lMl`MVn!;m!3PfVY1S3-+^Md~*qG z@kBQ+KvSu)A1qaRG;7V}s zn$mgoo9phX60(%6sbno>z*GiSl->0X;BNi!UI9w90c_rEHQ6^mvf@vy`2$xozjQB5 z+)bF^)8GGZ9np$)U7>sbnd|#J0hSWtNC&r+36J==$38^k{6}tpJv(sF$-!j*+Brj`)OjC%iB^3uXbO=OSJ)z;T9`q{S$-lf$CgjmC;#I{@pbVoat7ns_Li zSC{|zi0433B=^OHGfJ&-gh`CN6(g-RH`ekRMKzu;mkf=v>LzV1pU(Br68$?WKY*(2 zv*gHgIa0SFDOFWdmb%MScUkI?sSe$*EUWu1^^mC^dh*q>dg7U4{NjU9@Cf z@1Xl%+&3Ho4?|*3;vUAsIe~j5pqzA1auOJ>p|7Y~$)=C>ifp6t zDZN~xtVX51Fip-&bFQt2Aun43BqJ-(LB5bPa~|gO8I?N#x3RdDiG5SpD{r3lN6@f| z=qPNoyHU0gbD-2apNLW$J*+&5F#F+fkO?~!b~0%?4YKHe_jO^)ZAa}dH+;9Lb#Q$7Fi8JKDSD$VZy07C~1>;M1& diff --git a/python/test/Cilent/__pycache__/__init__.cpython-312.pyc b/python/test/Cilent/__pycache__/__init__.cpython-312.pyc index 8d1c67c601e56e8120db6015d47943a55f832608..c0d0e3a3c5289ca503695ad84dfeb421b89a9220 100644 GIT binary patch delta 1523 zcmbtUZEO=|9DnY)-nDCc>&x&C_RL7#3Elp#+TkfTrq)&7d&|h#`E?_@&o{bZPd%FWuH}bI|z7=>NHE=As{z@Z|3Q z^85du-*eCZxx2e{=bLMP;W#sbF=VShS}~<+W7O8vShHC&oJEJ(GmMA*dRjJ6l{Cv- zvor%waS_vW{aeP0H&s|j7O!VZ>rf3w%)JgLAxC;18+%p*cQ0@zT7|P!L*Gp|v3RRl#2nL{$*ygjWkfSQWx^ zLT~ze)3fPOmfKkak~G{8?qY;4p?4{X+61YDk8g*y{(zj;a)XxdN&W0%Bu2uC^i8&@ zBLPf(yhV2Kk1=XQ=qsWPgFKv4q7#} z1JJSOC2no+wWOO@kYrm!>{Z0EhB$TP6IQZneNH~=GkEGkN)xQ3G=M1@q-Y{qh6s&V z!7N?AS-So4;>1+x!%xe9!qCw22a~01lX^{oS3&2NX2zGM-%pl5y1YCyQ`TT7)E2LP zxp?k}(!}iY%xBBrd|tZno#G)=cv90|V_n|7bURg=nsh(sE*e8}A{0F#k63`?HtVxU z0SBZirY}Y?Q+G5TKQ*jK9)kk>Br2MNay%X#kUNJ@7qQq?vA8|e`64#9*#J!1+5=u87AlWgj=vka(B2Ez3 zFhcApoWu_i^3p!~cjEjsOd}4!0Nq8v0R095tIo?qT0w~GqhSlRyOby3OhO)Gdezk4 zq3ng^3#4J%Au9|3njt~F9?V&^idp73wQ4squTZN_VjnmyOdp+oy=E)*LVB!b i!YQLT`DRvVilP?Kh6U90kZqy*=&9CBtb$0St@IZbxSDAI delta 271 zcmeB?KP1d|nwOW00SLMZCDWoFeU^7sb`Di#uNwcfKy}b5Y#q zvbf)M@sNw+A(zF&CLiE1W%ko#o4k?Fc=Ag=X$cXa(Jc(0nAlje89xcIvwAUp76Orz wmH1VpbV2rtf(VeZUmP~M`6;D2sdh#BlN0zQg(Vri7$<1W$ot9wVu2L`09ZXpCIA2c diff --git a/python/test/Server/Database_formula.py b/python/test/Server/Database_formula.py index 8378598..f35b7b1 100644 --- a/python/test/Server/Database_formula.py +++ b/python/test/Server/Database_formula.py @@ -17,6 +17,9 @@ class database: self.link_database_thread = threading.Thread(target=self.link_database) self.link_database_thread.start() + # 创建一个互斥锁对象 + self.lock = threading.Lock() + def link_database(self): while not self.database_state: time.sleep(1) @@ -47,13 +50,29 @@ class database: self.database_state = False print(a) - def sign_account(self, Id: int, password): + def sign_account(self, NetName, Password): + if self.database_state: + # 获取锁 + self.lock.acquire() + try: + self.database_cursor.execute("insert into Account(NetName, Password)" + f"values ('{NetName}','{Password}')") + self.database_conn.commit() + self.database_cursor.execute(f"SELECT SCOPE_IDENTITY() AS LastInsertedId;") + Id = self.database_cursor.fetchone()[0] + # 释放锁 + self.lock.release() + return {"Id": int(Id), "NetName": NetName, "Password": Password} + except pymssql as a: + self.lock.release() + self.database_state = False + return False + + def alter_state_database(self, Id: int, sate): if self.database_state: try: - self.database_cursor.execute(f"select Id,Password from Account where Id = {Id}") - return 0 + self.database_cursor.execute(f"update Account set State = N'{sate}' where Id = {Id}") + self.database_conn.commit() except pymssql as a: self.database_state = False print(a) - return 1 - diff --git a/python/test/Server/Session_client.py b/python/test/Server/Session_client.py index a1b3730..daae258 100644 --- a/python/test/Server/Session_client.py +++ b/python/test/Server/Session_client.py @@ -1,6 +1,5 @@ import time import socket -import random import json import threading @@ -27,31 +26,43 @@ class link_client: pass def send_client(self, client_socket, genre, target, content): - if client_socket in self.client_socket_dict: - 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 Exception as a: - print("发送错误:" + str(a)) + 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): - while True: + state = True + while state: try: data_json = client_socket.recv(1024).decode('utf-8') data = json.loads(data_json) - print(data_json["genre"]) - if data_json["genre"] == '登录': + if data["genre"] == '登录': content = self.pick_data(data) - print(content['state']) - if content['state'] == 0: - self.client_socket_dict[content['user']] = client_socket - print(self.client_socket_dict[content['user']]) + 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: - client_socket.close() + 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/__init__.py b/python/test/Server/__init__.py index 33fd2e8..16edef0 100644 --- a/python/test/Server/__init__.py +++ b/python/test/Server/__init__.py @@ -11,6 +11,14 @@ class Server_main(link_client, database): account_state = None if data['genre'] == '登录': account_state = self.check_account_state(data['data']['account'], data['data']['password']) - return {"state": account_state, "user": data['data']['account']} + return account_state elif data['genre'] == '注册': - pass + 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/test.py b/python/test/test.py index c96fbb2..3706489 100644 --- a/python/test/test.py +++ b/python/test/test.py @@ -1,52 +1,26 @@ import wx +app = wx.App() -class MultiPanelFrame(wx.Frame): - def __init__(self): - super().__init__(None, title="Multi Panel Example", size=(400, 300)) +# 创建一个Frame +frame = wx.Frame(None, -1, '多行文本弹窗示例', size=(300, 200)) +frame.Center() - self.panel1 = wx.Panel(self) - self.panel2 = wx.Panel(self) - self.panel3 = wx.Panel(self) +# 创建一个Panel +panel = wx.Panel(frame, -1) - self.panel1.SetBackgroundColour(wx.Colour(255, 0, 0)) # Red - self.panel2.SetBackgroundColour(wx.Colour(0, 255, 0)) # Green - self.panel3.SetBackgroundColour(wx.Colour(0, 0, 255)) # Blue +# 创建一个按钮 +button = wx.Button(panel, -1, '点击弹窗', pos=(100, 50)) - self.sizer = wx.BoxSizer(wx.VERTICAL) - self.sizer.Add(self.panel1, 1, wx.EXPAND) - self.sizer.Add(self.panel2, 1, wx.EXPAND) - self.sizer.Add(self.panel3, 1, wx.EXPAND) +# 定义按钮的点击事件处理函数 +def on_button_click(event): + message = "这是一个多行文本的弹窗。\n第二行文本。\n第三行文本。" + dlg = wx.MessageDialog(frame, message, '多行文本提示', wx.OK | wx.ICON_INFORMATION) + dlg.ShowModal() + dlg.Destroy() - self.panel1.Show() - self.panel2.Hide() - self.panel3.Hide() +# 绑定按钮的点击事件 +button.Bind(wx.EVT_BUTTON, on_button_click) - self.SetSizer(self.sizer) - - self.Bind(wx.EVT_CHAR_HOOK, self.on_key_press) - - def on_key_press(self, event): - key = event.GetKeyCode() - if key == ord('1'): - self.panel1.Show() - self.panel2.Hide() - self.panel3.Hide() - self.Layout() - elif key == ord('2'): - self.panel1.Hide() - self.panel2.Show() - self.panel3.Hide() - self.Layout() - elif key == ord('3'): - self.panel1.Hide() - self.panel2.Hide() - self.panel3.Show() - self.Layout() - - -if __name__ == "__main__": - app = wx.App() - frame = MultiPanelFrame() - frame.Show() - app.MainLoop() +frame.Show() +app.MainLoop()