准备获取联系人列表(数据库),客户端重复发送
This commit is contained in:
parent
8c212e3803
commit
c93c289ef4
0
python/test/1000/info.csv
Normal file
0
python/test/1000/info.csv
Normal file
|
@ -3,12 +3,17 @@ import wx.aui
|
|||||||
import wx.lib.scrolledpanel as scrolled
|
import wx.lib.scrolledpanel as scrolled
|
||||||
import time
|
import time
|
||||||
import multiprocessing
|
import multiprocessing
|
||||||
|
import os
|
||||||
|
import csv
|
||||||
|
from Client.Transmission.Process_Client import ProcessClient
|
||||||
|
|
||||||
|
|
||||||
class ChatFrame(wx.Frame):
|
class ChatFrame(wx.Frame, ProcessClient):
|
||||||
def __init__(self):
|
def __init__(self, Id):
|
||||||
super().__init__(None, size=(800, 600), title="客户端")
|
wx.Frame().__init__(None, size=(800, 600), title="账号: " + str(Id))
|
||||||
|
ProcessClient.__init__(self)
|
||||||
|
|
||||||
|
self.detection_data(Id)
|
||||||
|
|
||||||
ChatMain_Panel = wx.Panel(self, style=wx.BORDER_SUNKEN)
|
ChatMain_Panel = wx.Panel(self, style=wx.BORDER_SUNKEN)
|
||||||
ChatMain_box = wx.BoxSizer(wx.HORIZONTAL)
|
ChatMain_box = wx.BoxSizer(wx.HORIZONTAL)
|
||||||
@ -113,6 +118,30 @@ class ChatFrame(wx.Frame):
|
|||||||
self.find_page.Show()
|
self.find_page.Show()
|
||||||
self.Layout()
|
self.Layout()
|
||||||
|
|
||||||
|
def detection_data(self, Id):
|
||||||
|
root_path = rf'.\{Id}'
|
||||||
|
info_path = root_path + r'\info.csv'
|
||||||
|
file_root_path = root_path + r'\file'
|
||||||
|
other_file_path = file_root_path + r'\other'
|
||||||
|
image_file_path = file_root_path + r'\image'
|
||||||
|
if not os.path.isdir(root_path):
|
||||||
|
os.mkdir(root_path)
|
||||||
|
if not os.path.isdir(file_root_path):
|
||||||
|
os.mkdir(file_root_path)
|
||||||
|
os.mkdir(other_file_path)
|
||||||
|
os.mkdir(image_file_path)
|
||||||
|
if not os.path.exists(info_path):
|
||||||
|
with open(info_path, 'w', encoding='utf-8') as f:
|
||||||
|
pass
|
||||||
|
with open(info_path, 'r+', encoding='utf-8') as info:
|
||||||
|
if info.tell() == 0:
|
||||||
|
date = None
|
||||||
|
target = "服务器"
|
||||||
|
genre = "数据更新"
|
||||||
|
content = date
|
||||||
|
data = {"genre": genre, "target": target, "content": content}
|
||||||
|
self.Process_client_send("Session_server", "send_server", data)
|
||||||
|
|
||||||
class ChatPage(wx.Panel):
|
class ChatPage(wx.Panel):
|
||||||
def __init__(self, parent):
|
def __init__(self, parent):
|
||||||
wx.Panel.__init__(self, parent, style=wx.BORDER_SUNKEN)
|
wx.Panel.__init__(self, parent, style=wx.BORDER_SUNKEN)
|
||||||
@ -196,6 +225,7 @@ class ChatFrame(wx.Frame):
|
|||||||
chat_panel.SetSizer(chat_box)
|
chat_panel.SetSizer(chat_box)
|
||||||
|
|
||||||
self.chat_tab.AddPage(chat_panel, Remark, select=True)
|
self.chat_tab.AddPage(chat_panel, Remark, select=True)
|
||||||
|
self.GetParent().contact_windows[Id] = chat_panel
|
||||||
|
|
||||||
class ConnectPage(wx.Panel):
|
class ConnectPage(wx.Panel):
|
||||||
def __init__(self, parent):
|
def __init__(self, parent):
|
||||||
|
@ -9,9 +9,9 @@ from Client.Transmission.Process_Client import ProcessClient
|
|||||||
from .Chat_main import ChatFrame
|
from .Chat_main import ChatFrame
|
||||||
|
|
||||||
|
|
||||||
def open_chat_window():
|
def open_chat_window(Id):
|
||||||
app = wx.App()
|
app = wx.App()
|
||||||
ChatFrame().Show()
|
ChatFrame(Id).Show()
|
||||||
app.MainLoop()
|
app.MainLoop()
|
||||||
|
|
||||||
|
|
||||||
@ -20,6 +20,7 @@ class LoginFrame(wx.Frame, ProcessClient):
|
|||||||
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))
|
||||||
ProcessClient.__init__(self)
|
ProcessClient.__init__(self)
|
||||||
|
|
||||||
|
|
||||||
current_file_path = __file__
|
current_file_path = __file__
|
||||||
current_file_name = os.path.basename(current_file_path).split('.')[0]
|
current_file_name = os.path.basename(current_file_path).split('.')[0]
|
||||||
self.Process_client_send("Server", "Name", current_file_name)
|
self.Process_client_send("Server", "Name", current_file_name)
|
||||||
@ -107,10 +108,10 @@ class LoginFrame(wx.Frame, ProcessClient):
|
|||||||
|
|
||||||
def login_page_receive(self, receive_content):
|
def login_page_receive(self, receive_content):
|
||||||
if receive_content["genre"] == '登录':
|
if receive_content["genre"] == '登录':
|
||||||
match receive_content["data"]:
|
match receive_content["data"]['status']:
|
||||||
case 0:
|
case 0:
|
||||||
multiprocessing.Process(target=open_chat_window).start()
|
self.Destroy()
|
||||||
self.Close()
|
multiprocessing.Process(target=open_chat_window, args=(receive_content["data"]['account'],)).start()
|
||||||
case -1:
|
case -1:
|
||||||
wx.MessageBox('重复登录', '登录', wx.OK | wx.ICON_INFORMATION)
|
wx.MessageBox('重复登录', '登录', wx.OK | wx.ICON_INFORMATION)
|
||||||
case 1:
|
case 1:
|
||||||
@ -130,7 +131,9 @@ class LoginFrame(wx.Frame, ProcessClient):
|
|||||||
target = "服务器"
|
target = "服务器"
|
||||||
genre = "登录"
|
genre = "登录"
|
||||||
data = {"genre": genre, "target": target, "content": content}
|
data = {"genre": genre, "target": target, "content": content}
|
||||||
self.Process_client_send("Session_server", "send_server", data)
|
if account and password:
|
||||||
|
self.Process_client_send("Session_server", "send_server", data)
|
||||||
|
time.sleep(3)
|
||||||
|
|
||||||
def send_register_button(self, event):
|
def send_register_button(self, event):
|
||||||
if self.server_status:
|
if self.server_status:
|
||||||
@ -140,7 +143,9 @@ class LoginFrame(wx.Frame, ProcessClient):
|
|||||||
target = "服务器"
|
target = "服务器"
|
||||||
genre = "注册"
|
genre = "注册"
|
||||||
data = {"genre": genre, "target": target, "content": content}
|
data = {"genre": genre, "target": target, "content": content}
|
||||||
self.Process_client_send("Session_server", "send_server", data)
|
if account and password:
|
||||||
|
self.Process_client_send("Session_server", "send_server", data)
|
||||||
|
time.sleep(3)
|
||||||
|
|
||||||
def Process_client_pick(self, data):
|
def Process_client_pick(self, data):
|
||||||
if data['target'] in ['ALL', 'Login']:
|
if data['target'] in ['ALL', 'Login']:
|
||||||
@ -151,6 +156,7 @@ class LoginFrame(wx.Frame, ProcessClient):
|
|||||||
self.login_page_receive(data['content'])
|
self.login_page_receive(data['content'])
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class LoginPanel(wx.Panel):
|
class LoginPanel(wx.Panel):
|
||||||
def __init__(self, parent):
|
def __init__(self, parent):
|
||||||
super().__init__(parent)
|
super().__init__(parent)
|
||||||
|
@ -89,21 +89,29 @@ class database(ProcessClient):
|
|||||||
if data['target'] in ['ALL', 'Database_formula']:
|
if data['target'] in ['ALL', 'Database_formula']:
|
||||||
match data['function']:
|
match data['function']:
|
||||||
case 'check_account_state':
|
case 'check_account_state':
|
||||||
client_socket = data['content']['client_socket']
|
client_id = data['content']['client_id']
|
||||||
account = data['content']['account']
|
account = data['content']['account']
|
||||||
password = data['content']['password']
|
password = data['content']['password']
|
||||||
|
|
||||||
status = self.check_account_state(account, password)
|
status = self.check_account_state(account, password)
|
||||||
content = {"account": account, "status": status}
|
content = {"account": account, "status": status}
|
||||||
content = {"client_socket": client_socket, "genre": "客户端", "target": "登录", "content": content}
|
content = {"client_id": client_id, "target": "客户端", "genre": "登录", "data": content}
|
||||||
|
|
||||||
self.Process_client_send('Session_client', 'send_client', content)
|
self.Process_client_send('Session_client', 'send_client', content)
|
||||||
|
|
||||||
case 'sign_account':
|
case 'sign_account':
|
||||||
client_socket = data['content']['client_socket']
|
client_id = data['content']['client_id']
|
||||||
account = data['content']['account']
|
account = data['content']['account']
|
||||||
password = data['content']['password']
|
password = data['content']['password']
|
||||||
|
|
||||||
info = self.sign_account(account, password)
|
info = self.sign_account(account, password)
|
||||||
content = {"client_socket": client_socket, "genre": "客户端", "target": "注册", "content": info}
|
content = {"client_id": client_id, "target": "客户端", "genre": "注册", "data": info}
|
||||||
self.Process_client_send('Session_client', 'send_client', content)
|
self.Process_client_send('Session_client', 'send_client', content)
|
||||||
|
|
||||||
|
case 'alter_state_database':
|
||||||
|
self.alter_state_database(data['content']['Id'], data['content']['sate'])
|
||||||
|
|
||||||
|
|
||||||
|
def detection_data(self,date):
|
||||||
|
if date is None:
|
||||||
|
self.database_cursor.execute(f"update Account set State = N'{sate}' where Id = {Id}")
|
@ -1,4 +1,6 @@
|
|||||||
import os
|
import os
|
||||||
|
import random
|
||||||
|
import string
|
||||||
import time
|
import time
|
||||||
import socket
|
import socket
|
||||||
import json
|
import json
|
||||||
@ -15,6 +17,7 @@ class link_client(ProcessClient):
|
|||||||
self.server_socket.bind((self.server_host, self.server_port))
|
self.server_socket.bind((self.server_host, self.server_port))
|
||||||
self.server_socket.listen()
|
self.server_socket.listen()
|
||||||
self.client_socket_dict = {}
|
self.client_socket_dict = {}
|
||||||
|
self.client_max_id = 0
|
||||||
|
|
||||||
current_file_path = __file__
|
current_file_path = __file__
|
||||||
current_file_name = os.path.basename(current_file_path).split('.')[0]
|
current_file_name = os.path.basename(current_file_path).split('.')[0]
|
||||||
@ -27,12 +30,16 @@ class link_client(ProcessClient):
|
|||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
client_socket, client_address = self.server_socket.accept()
|
client_socket, client_address = self.server_socket.accept()
|
||||||
|
self.client_max_id += 1
|
||||||
|
client_id = ''.join(random.choice(string.ascii_letters) for _ in range(10)) + str(self.client_max_id)
|
||||||
|
self.client_socket_dict[client_id] = client_socket
|
||||||
recv_client_Thread = threading.Thread(target=self.recv_client(client_socket), daemon=True)
|
recv_client_Thread = threading.Thread(target=self.recv_client(client_socket), daemon=True)
|
||||||
recv_client_Thread.start()
|
recv_client_Thread.start()
|
||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def send_client(self, client_socket, genre, target, content):
|
def send_client(self, client_id, genre, target, content):
|
||||||
|
client_socket = self.client_socket_dict[client_id]
|
||||||
try:
|
try:
|
||||||
data = {"genre": genre, "target": target, "data": content,
|
data = {"genre": genre, "target": target, "data": content,
|
||||||
"datetime": time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())}
|
"datetime": time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())}
|
||||||
@ -41,16 +48,18 @@ class link_client(ProcessClient):
|
|||||||
except:
|
except:
|
||||||
client_socket.close()
|
client_socket.close()
|
||||||
|
|
||||||
def pick_data(self, client_socket, data):
|
def pick_data(self, client_id, data):
|
||||||
match data['genre']:
|
match data['genre']:
|
||||||
case '登录':
|
case '登录':
|
||||||
content = {'client_socket': client_socket, 'account': data['data']['account'],
|
content = {'client_id': client_id, 'account': data['data']['account'],
|
||||||
'password': data['data']['password']}
|
'password': data['data']['password']}
|
||||||
self.Process_client_send("Database_formula", "check_account_state", content)
|
self.Process_client_send("Database_formula", "check_account_state", content)
|
||||||
case '注册':
|
case '注册':
|
||||||
content = {'client_socket': client_socket, 'account': data['data']['account'],
|
content = {'client_id': client_id, 'account': data['data']['account'],
|
||||||
'password': data['data']['password']}
|
'password': data['data']['password']}
|
||||||
self.Process_client_send("Database_formula", "sign_account", content)
|
self.Process_client_send("Database_formula", "sign_account", content)
|
||||||
|
case '数据更新':
|
||||||
|
self.Process_client_send("Database_formula", "sign_account", data['data'])
|
||||||
|
|
||||||
def recv_client(self, client_socket):
|
def recv_client(self, client_socket):
|
||||||
state = True
|
state = True
|
||||||
@ -58,31 +67,48 @@ class link_client(ProcessClient):
|
|||||||
try:
|
try:
|
||||||
data_json = client_socket.recv(1024).decode('utf-8')
|
data_json = client_socket.recv(1024).decode('utf-8')
|
||||||
data = json.loads(data_json)
|
data = json.loads(data_json)
|
||||||
self.pick_data(client_socket, data)
|
client_Id = self.find_client(client_socket, None)
|
||||||
|
self.pick_data(client_Id, data)
|
||||||
except:
|
except:
|
||||||
try:
|
try:
|
||||||
client_socket.close()
|
client_socket.close()
|
||||||
finally:
|
finally:
|
||||||
state = False
|
state = False
|
||||||
for key, value in self.client_socket_dict.items():
|
self.find_client(client_socket, None)
|
||||||
if value == client_socket:
|
client_id = self.find_client(client_socket, None)
|
||||||
del self.client_socket_dict[key]
|
del self.client_socket_dict[client_id]
|
||||||
self.Process_client_send("Database_formula", "alter_state_database",
|
if client_id.isdigit():
|
||||||
{"Id": key, "sate": "在线"})
|
self.Process_client_send("Database_formula", "alter_state_database",
|
||||||
break
|
{"Id": client_id, "sate": 0})
|
||||||
|
|
||||||
def Process_client_pick(self, data):
|
def Process_client_pick(self, data):
|
||||||
if data['target'] in ['ALL', 'Session_client']:
|
if data['target'] in ['ALL', 'Session_client']:
|
||||||
match data['function']:
|
match data['function']:
|
||||||
case 'send_client':
|
case 'send_client':
|
||||||
client_socket = data['content']['client_socket']
|
client_id = data['content']['client_id']
|
||||||
genre = data['content']['genre']
|
genre = data['content']['genre']
|
||||||
target = data['content']['target']
|
target = data['content']['target']
|
||||||
content = data['content']['content']
|
content = data['content']['data']
|
||||||
self.send_client(client_socket, genre, target, content)
|
if data['content']['genre'] == '登录':
|
||||||
if target == '登录':
|
if content['account'] not in self.client_socket_dict:
|
||||||
if data['content']['status'] == 0:
|
if content['status'] == 0:
|
||||||
self.client_socket_dict[data['content']['account']] = data['data']['client_socket']
|
client_socket = self.find_client(None, client_id)
|
||||||
self.Process_client_send("Database_formula", "alter_state_database",
|
del self.client_socket_dict[client_id]
|
||||||
{"Id": data['content']['account'],
|
client_id = content['account']
|
||||||
"sate": "在线"})
|
self.client_socket_dict[client_id] = client_socket
|
||||||
|
if client_id.isdigit():
|
||||||
|
self.Process_client_send("Database_formula", "alter_state_database",
|
||||||
|
{"Id": client_id, "sate": 1})
|
||||||
|
else:
|
||||||
|
return
|
||||||
|
self.send_client(client_id, genre, target, content)
|
||||||
|
|
||||||
|
def find_client(self, client_socket, client_id):
|
||||||
|
if client_id is None:
|
||||||
|
for key, value in self.client_socket_dict.items():
|
||||||
|
if value == client_socket:
|
||||||
|
return key
|
||||||
|
elif client_socket is None:
|
||||||
|
for key, value in self.client_socket_dict.items():
|
||||||
|
if key == client_id:
|
||||||
|
return value
|
||||||
|
Loading…
Reference in New Issue
Block a user