본문 바로가기
socket

python - multicast receive with esp8266

by space father python 2023. 1. 2.
반응형

https://arduino-dds.tistory.com/entry/esp8266-multicast-with-python-pc

 

esp8266 - multicast with python pc

https://python-dds.tistory.com/entry/python-multicast-receive-with-esp8266 https://python-dds.tistory.com/entry/python-multicast-send-with-esp8266 python - multicast send with esp8266 Code 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 import socket MCAST_GRP = '224.

arduino-dds.tistory.com

Code

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import socket
import struct
 
MCAST_GRP = '224.1.1.1'
MCAST_PORT = 5123
 
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
 
sock.bind(('', MCAST_PORT))
mreq = struct.pack("4sl", socket.inet_aton(MCAST_GRP), socket.INADDR_ANY)
 
sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)
 
while True:
    data, addr = sock.recvfrom(1024)
    print(f'received message: {data} {addr}')
cs

 

Result

received message: b'esp8266_(5123): 7' ('192.168.10.15', 5123)
received message: b'esp8266_(5123): 8' ('192.168.10.15', 5123)
received message: b'esp8266_(5123): 9' ('192.168.10.15', 5123)
received message: b'esp8266_(5123): 10' ('192.168.10.15', 5123)
received message: b'esp8266_(5123): 11' ('192.168.10.15', 5123)
received message: b'esp8266_(5123): 12' ('192.168.10.15', 5123)
received message: b'python_(5123)\n' ('192.168.10.61', 51838)
received message: b'python_(5123)\n' ('192.168.10.61', 51838)
received message: b'esp8266_(5123): 13' ('192.168.10.15', 5123)
received message: b'esp8266_(5123): 14' ('192.168.10.15', 5123)
received message: b'python_(5123)\n' ('192.168.10.61', 45196)
received message: b'python_(5123)\n' ('192.168.10.61', 45196)
received message: b'esp8266_(5123): 15' ('192.168.10.15', 5123)
received message: b'esp8266_(5123): 16' ('192.168.10.15', 5123)
received message: b'python_(5123)\n' ('192.168.10.61', 35352)
received message: b'python_(5123)\n' ('192.168.10.61', 35352)
received message: b'esp8266_(5123): 17' ('192.168.10.15', 5123)
received message: b'esp8266_(5123): 18' ('192.168.10.15', 5123)
received message: b'esp8266_(5123): 19' ('192.168.10.15', 5123)
received message: b'esp8266_(5123): 20' ('192.168.10.15', 5123)
received message: b'esp8266_(5123): 21' ('192.168.10.15', 5123)

 

'socket' 카테고리의 다른 글

Python - UDP (send, receive)  (0) 2023.01.05
Python - all ip check in pc  (0) 2023.01.04
python - multicast send with esp8266  (0) 2023.01.02