1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#!/usr/bin/python python 
# -*- coding:UTF-8 -*-
# UDP 广播接收
from socket import *

HOST = '0.0.0.0'
PORT = 6681
BUFSIZE = 1024

broadcastrecv = socket(AF_INET, SOCK_DGRAM)
broadcastrecv.bind((HOST,PORT))
print('wating...')
while True:
data, addr = broadcastrecv.recvfrom(BUFSIZE)
print('recv: %s %s'%(addr,data) )

broadcastrecv.close()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#!/usr/bin/python python 
# -*- coding:UTF-8 -*-
# UDP广播发送
from socket import *

HOST = '192.168.3.255'
PORT = 6681
BUFSIZE = 1024
ADDR = (HOST, PORT)

broadcast = socket(AF_INET, SOCK_DGRAM)
broadcast.setsockopt(SOL_SOCKET, SO_BROADCAST, 1)
while True:
data = raw_input('>')
if not data:
break
print("send: %s"%data)
broadcast.sendto(data, ADDR)

broadcast.close()