1. 程式人生 > >如何使用 Python 編寫基於 UDP 協議的聊天工具

如何使用 Python 編寫基於 UDP 協議的聊天工具

import socket
from sys import exit

s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

HOST = '10.1.73.12'
PORT = 10888

s.bind((HOST, PORT))

while True:
    data,addr = s.recvfrom(1024)
    print("Recv-data", data.decode())
    s.sendto(data, addr)
import socket
from sys import exit

s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

HOST = '10.1.73.12'
PORT = 10888

s.connect((HOST, PORT))

while True:
    data = input("Input Data: ")
    s.sendto(data.encode('utf-8'), (HOST,PORT))
    data,addr = s.recvfrom(1024)
    print("Recv-Data: ", data.decode('utf-8'))