Python网络编程-UDP 写聊天界面

from socket import *

# 先运行这个文件
print('----欢迎来到<傻子彤>的聊天窗口----')
recv_socket = socket(AF_INET, SOCK_DGRAM)
recv_socket.bind(('', 8888))
while True:
    content = recv_socket.recvfrom(1024)
    print('小可爱回复:%s' % (content[0].decode('utf-8')))
    if not content[0]:
        print('对方已下线-----')
        break

    data = input('傻子彤说:')
    recv_socket.sendto(data.encode('utf-8'), ('192.168.11.57', 9999))  #IP改成自己的
    if not data:
        print('悄悄的退出中-----')
        break

recv_socket.close()
from socket import *

# 再运次这个文件
print('----欢迎来到<小可爱>的聊天窗口----')
send_socket = socket(AF_INET, SOCK_DGRAM)
address = ('192.168.11.57', 8888)  #IP改成自己的
send_socket.bind(('', 9999))
while True:
    data = input('小可爱说:')
    send_socket.sendto(data.encode('utf-8'), address)
    if not data:
        print('悄悄的退出中-----')
        break

    msg = send_socket.recvfrom(1024)
    print('傻子彤回复:%s' % (msg[0].decode('utf-8')))
    if not msg[0]:
        print('对方已下线-----')
        break

send_socket.close()

你可能感兴趣的:(Python网络编程-UDP 写聊天界面)