60 lines
1.6 KiB
Python
60 lines
1.6 KiB
Python
import json
|
|
import socket
|
|
|
|
|
|
def chat(username, chat_obj, content):
|
|
'''
|
|
发送聊天信息
|
|
'''
|
|
pass
|
|
|
|
|
|
# 创建socket
|
|
ip_port = ("127.0.0.1", 9000)
|
|
sk = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
|
|
|
# 选择菜单
|
|
while 1:
|
|
print("menu".center(30, "="))
|
|
print("1.login".center(30, " "))
|
|
print("2.register".center(30, " "))
|
|
print("=".center(30, "="))
|
|
choice = input(">>>")
|
|
|
|
if choice == "1":
|
|
username = input("用户名:")
|
|
password = input("密码:")
|
|
data = {
|
|
"option": "login",
|
|
"username": username,
|
|
"password": password
|
|
}
|
|
sk.sendto(json.dumps(data).encode("utf-8"), ip_port)
|
|
msg, addr = sk.recvfrom(1024)
|
|
# {"status": 1}
|
|
if json.loads(msg.decode("utf-8"))["status"]:
|
|
chat_obj = input("输入收件人:")
|
|
content = input("输入内容:")
|
|
chat(username, chat_obj, content)
|
|
continue
|
|
else:
|
|
print("登录失败")
|
|
continue
|
|
elif choice == "2":
|
|
username = input("用户名:")
|
|
password = input("密码:")
|
|
data = {
|
|
"option": "register",
|
|
"username": username,
|
|
"password": password
|
|
}
|
|
sk.sendto(json.dumps(data).encode("utf-8"), ip_port)
|
|
msg, addr = sk.recvfrom(1024)
|
|
# {"status": 1}
|
|
if json.loads(msg.decode("utf-8"))["status"]:
|
|
print("注册成功")
|
|
continue
|
|
else:
|
|
print(json.loads(msg.decode("utf-8"))["msg"])
|
|
continue
|