first commit

This commit is contained in:
2025-09-16 16:07:48 +08:00
commit 07a4b175cb
4 changed files with 129 additions and 0 deletions

41
server.py Normal file
View File

@@ -0,0 +1,41 @@
import json
import socket
# 创建socket
sk = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sk.bind(("0.0.0.0", 9000))
active = []
db = {}
chat_msg = {}
while 1:
print(f"当前活跃用户:{active} 当前注册用户:{db}")
data, addr = sk.recvfrom(2048)
data = json.loads(data)
res = {"status": 0}
if data["option"] == "login":
username = data["username"]
password = data["password"]
if username in db:
if db[username] == password:
res = {"status": 1}
active.append(data["username"])
else:
res = {"status": 0}
else:
res = {"status": 0}
elif data["option"] == "register":
username = data["username"]
password = data["password"]
if username in db:
res = {"status": 0, "msg": "用户名已存在"}
else:
db[username] = password
res = {"status": 1, "msg": "注册成功"}
elif data["option"] == "chat":
# 接受聊天信息,并且存入聊天临时字典
pass
elif data["option"] == "hello":
# 接受客户端的存活状态,如果发现临时聊天字典有该用户的未收消息,发送过去
pass
sk.sendto(json.dumps(res).encode("utf-8"), addr)