import socket
def send_msg():
"""发送数据"""
dest_ip = input("请输入对方的ip: ")
dest_port = int(input("请输入对方的端口号: "))
send_data = input("请输入发送的消息: ")
udp_socket.sendto(send_data.encode("utf-8"), (dest_ip, dest_prot))
def recv_msg(udp_socket):
"""接收数据"""
recv_data = udp_socket.recvfrom(1024)
print("%s:%s"%(str(recv_data[1], recv_data[0].decode("utf-8"))))
def main():
# 创建套接字
udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# 绑定信息
udp_socket.bind(("", 7788))
# 循环来发送和接收数据
while True:
print "xxx聊天器"
print "1.发送数据"
print "2.接收数据"
print "0.退出系统"
op = input("请输入功能: ")
if "1" == op:
# 发送数据
send_msg(udp_socket)
elif "2" == op:
# 接收数据
recv_msg(udp_socket)
elif "0" == op:
break
else:
print "输入有误"
if __name__ == "__main__":
main()
网友评论