进行相应的文档调试就可以实现,具体的实现步骤如下:
import socket
import threading
import time
host = 'localhost'
port = 7787
running = False
MAX_PACKET_SIZE = 1024
clients = []
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind((host, port))
s.setblocking(0)
running = True
print "server started..."
while running:
try:
data, address = s.recvfrom(MAX_PACKET_SIZE)
if 'quit' in str(data):
running = False
print 'server stopped...'
if address not in clients:
clients.append(address)
print time.ctime(time.time()) + str(address) + ": :" + str(data)
for client in clients:
s.sendto(data, client)
except:
pass
running = False
s.close()
温馨提示:答案为网友推荐,仅供参考