码迷,mamicode.com
首页 > 编程语言 > 详细

python+soket实现UDP协议的局域网广播程序

时间:2015-01-26 06:31:13      阅读:222      评论:0      收藏:0      [点我收藏+]

标签:

 

 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 = 1060
10 
11 network = <broadcast>
12 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+soket实现UDP协议的局域网广播程序

标签:

原文地址:http://www.cnblogs.com/hhh5460/p/4249329.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!