Self-Improvement

[Python socket] Multicast Request/Receive 본문

프로그래밍/Python

[Python socket] Multicast Request/Receive

JoGeun 2020. 8. 27. 15:00
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
from socket import *
 
# Multicast IP, Port
MCAST_GRP = '224.0.0.251'
MCAST_PORT = 5353
 
# UDP Socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
 
# on this port, receives ALL multicast groups
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind(('', MCAST_PORT))
 
# Host IP Get
host = socket.gethostbyname(socket.gethostname())
 
# Multicast Request
sock.sendto("hello", (MCAST_GRP, MCAST_PORT))
 
# Multicast Receive setting
sock.setsockopt(socket.SOL_IP, socket.IP_ADD_MEMBERSHIP,socket.inet_aton(MCAST_GRP) + socket.inet_aton(host))
 
while 1:
    print(sock.recv(1500))
cs