Code _python to receive multicast data using Python

Source: Internet
Author: User
Tags socket
First of all, it's not what I wrote, it's what I found, but the others wrote it pretty well.
Copy Code code as follows:

# UDP Multicast examples, Hugo Vincent, 2005-05-14.
Import socket

def send (data, port=50000, addr= ' 239.192.1.100 '):
"" "Send (data[, port[, addr]])-multicasts a UDP datagram." "
# 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) # 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 datagram 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)
Except 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 data


Copy Code code as follows:

#!/usr/bin/env python
From socket import *from time import Sleep,time,ctime
Host = ' 229.0.0.15 ' PORT = 9999BUFSIZ = 1024ADDR = (host, PORT)
Udpclisock = socket (af_inet, SOCK_DGRAM)
data = ' t ' *200count = 0while (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.