Code for receiving multicast data using PYTHON

Source: Internet
Author: User

First, I declare that this is not what I wrote, but what I found, but what others wrote is quite good. Copy codeThe Code is as follows: # UDP multicast examples, Hugo Vincent, 2005-05-14.
Import socket

Def send (data, port = 50000, addr = '192.1.100 '):
"Send (data [, port [, addr])-multicasts a UDP datax ."""
# Create the socket
S = socket. socket (socket. AF_INET, socket. SOCK_DGRAM)
# Make the socket multicast-aware, and set TTL.
S. setsockopt (socket. IPPROTO_IP, socket. IP_MULTICAST_TTL, 20) # Change TTL (= 20) to suit
# Send the data
S. sendto (data, (addr, port ))

Def recv (port = 50000, addr = "239.192.1.100", buf_size = 1024 ):
"Recv ([port [, addr [, buf_size])-waits for a datax and returns the data ."""

# Create the socket
S = socket. socket (socket. AF_INET, socket. SOCK_DGRAM)

# Set some options to make it multicast-friendly
S. setsockopt (socket. SOL_SOCKET, socket. SO_REUSEADDR, 1)
Try:
S. setsockopt (socket. SOL_SOCKET, socket. SO_REUSEPORT, 1)
T AttributeError:
Pass # Some systems don't support SO_REUSEPORT
S. setsockopt (socket. SOL_IP, socket. IP_MULTICAST_TTL, 20)
S. setsockopt (socket. SOL_IP, socket. IP_MULTICAST_LOOP, 1)

# Bind to the port
S. bind ('', port ))

# Set some more multicast options
Intf = socket. gethostbyname (socket. gethostname ())
S. setsockopt (socket. SOL_IP, socket. IP_MULTICAST_IF, socket. inet_aton (intf) + socket. inet_aton ('0. 0.0.0 '))
S. setsockopt (socket. SOL_IP, socket. IP_ADD_MEMBERSHIP, socket. inet_aton (addr) + socket. inet_aton ('0. 0.0.0 '))

# Receive the data, then unregister multicast receive membership, then close the port
Data, sender_addr = s. recvfrom (buf_size)
S. setsockopt (socket. SOL_IP, socket. IP_DROP_MEMBERSHIP, socket. inet_aton (addr) + socket. inet_aton ('0. 0.0.0 '))
S. close ()
Return dataCopy codeThe Code is as follows :#! /Usr/bin/env python
From socket import * from time import sleep, time, ctime
HOST = '1970. 0.0.15 'PORT = 229 BUFSIZ = 9999 ADDR = (HOST, PORT)
UdpCliSock = socket (AF_INET, SOCK_DGRAM)
Data = 'T' * 200 count = 0 while (count <100): udpCliSock. sendto (data, ADDR) sleep (1) count = count + 1
UdpCliSock. close ()

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.