Python+Socket实现基于UDP协议的局域网广播功能示例
本文实例讲述了Python+Socket实现基于UDP协议的局域网广播功能。分享给大家供大家参考,具体如下:
服务器端:
# udp_gb_server.py
'''服务端(UDP协议局域网广播)'''
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
PORT = 1060
network = '<broadcast>'
s.sendto('Client broadcast message!'.encode('utf-8'), (network, PORT))客户端:
# udp_gb_client.py
'''客户端(UDP协议局域网广播)'''
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
PORT = 1060
s.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相关内容可查看本站专题:《Python Socket编程技巧总结》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总》
希望本文所述对大家Python程序设计有所帮助。
相关推荐
安得情怀似旧时 2020-03-04
missingmuch 2020-05-26
仁鱼 2020-02-12
DumbbellYang 2020-05-07
xx0cw 2020-05-07
SZStudy 2020-04-21
diediexiaomi 2020-04-10
OwenJi 2020-01-17
xx0cw 2019-12-13
Lovexinyang 2019-11-17
Jesuslovesme 2010-05-05
LeoHan 2015-08-11
linux爱好者 2010-11-18
thlm0 2019-10-21
batoom 2011-09-23
linuxvfast 2018-03-20
Fangxingt 2019-04-10
dasheng0 2018-03-20