首页 > 代码库 > python+soket实现UDP协议的局域网广播程序
python+soket实现UDP协议的局域网广播程序
1 # udp_gb_server.py 2 ‘‘‘服务端(UDP协议局域网广播)‘‘‘ 3 4 import socket 5 6 s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) 7 s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1) 8 9 PORT = 106010 11 network = ‘<broadcast>‘12 s.sendto(‘Client broadcast message!‘.encode(‘utf-8‘), (network, PORT))
# udp_gb_client.py‘‘‘客户端(UDP协议局域网广播)‘‘‘import sockets = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)PORT = 1060s.bind((‘‘, PORT))print(‘Listening for broadcast at ‘, s.getsockname())while True: data, address = s.recvfrom(65535) print(‘Server received from {}:{}‘.format(address, data.decode(‘utf-8‘)))
python+soket实现UDP协议的局域网广播程序
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。