import socket users={} host = '172.22.102.20' port = 9999 server_socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM) server_socket.bind((host,port)) server_socket.listen(1) server_socket.listen(1) print('等待连接...') client_socket,client_address=server_socket.accept() print(f'已连接:{client_address}') while True: message=client_socket.recv(1024).decode('utf-8') print(f'客户端:{message}') if not message: break if message.startswith('register'): # 处理注册请求 _, username, password = message.split() # 解析用户名和密码 users[username] = password # 保存用户信息 reply_message = f'注册成功,用户名为{username}' elif message.startswith('login'): # 处理登录请求 _, username, password = message.split() # 解析用户名和密码 if users.get(username) == password: # 验证用户身份 reply_message = f'登录成功,欢迎{username}!' else: reply_message = '用户名或密码错误,请重新输入。' reply_message=f'已收到:{message}' client_socket.sendall(reply_message.encode('utf-8')) client_socket.close() client_socket.close()这是服务器端的代码,import socket host='172.22.102.20' port=9999 client_socket=socket.socket(socket.AF_INET,socket.SOCK_STREAM) client_socket.connect((host,port)) while True: username = input('请输入用户名:') password = input('请输入密码:') message = f'register {username} {password}' client_socket.sendall(message.encode('utf-8')) message=input('请输入要发送的信息:') reply_message=client_socket.recv(1024).decode('utf-8') print(f'服务器:{reply_message}') client_socket.close()这是客户端的代码,在此基础上增加一个gui框架
时间: 2023-08-07 08:05:16 浏览: 182
好的,你需要选择一个GUI框架,比如Tkinter、PyQt等,然后将代码嵌入到GUI框架中。以下是一个基于Tkinter的简单示例:
```python
import tkinter as tk
import socket
host = '172.22.102.20'
port = 9999
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect((host, port))
class ChatApp(tk.Tk):
def __init__(self):
super().__init__()
self.title('Chat App')
self.geometry('400x500')
self.create_widgets()
def create_widgets(self):
self.message_listbox = tk.Listbox(self, height=20)
self.message_listbox.pack(pady=10)
self.username_entry = tk.Entry(self, width=20)
self.username_entry.pack(pady=5)
self.password_entry = tk.Entry(self, width=20, show='*')
self.password_entry.pack(pady=5)
self.register_button = tk.Button(self, text='注册', command=self.register)
self.register_button.pack(pady=5)
self.login_button = tk.Button(self, text='登录', command=self.login)
self.login_button.pack(pady=5)
self.message_entry = tk.Entry(self, width=30)
self.message_entry.pack(pady=10)
self.send_button = tk.Button(self, text='发送', command=self.send_message)
self.send_button.pack(pady=5)
def register(self):
username = self.username_entry.get()
password = self.password_entry.get()
message = f'register {username} {password}'
client_socket.sendall(message.encode('utf-8'))
reply_message = client_socket.recv(1024).decode('utf-8')
self.message_listbox.insert(tk.END, f'服务器:{reply_message}')
def login(self):
username = self.username_entry.get()
password = self.password_entry.get()
message = f'login {username} {password}'
client_socket.sendall(message.encode('utf-8'))
reply_message = client_socket.recv(1024).decode('utf-8')
self.message_listbox.insert(tk.END, f'服务器:{reply_message}')
def send_message(self):
message = self.message_entry.get()
client_socket.sendall(message.encode('utf-8'))
reply_message = client_socket.recv(1024).decode('utf-8')
self.message_listbox.insert(tk.END, f'服务器:{reply_message}')
self.message_entry.delete(0, tk.END)
if __name__ == '__main__':
chat_app = ChatApp()
chat_app.mainloop()
```
你可以将此代码保存为`.py`文件并运行,它将显示一个简单的GUI窗口,其中包含一个消息列表框、一个用户名输入框、一个密码输入框、一个注册按钮、一个登录按钮、一个消息输入框和一个发送按钮。当用户单击注册或登录按钮时,它将发送相应的消息到服务器,并将服务器的回复显示在消息列表框中。当用户输入一条消息并单击发送按钮时,它将发送该消息到服务器,并将服务器的回复显示在消息列表框中。
阅读全文