Python enables the sending and receiving of multicast data

Source: Internet
Author: User

In the project, the Ys Private protocol uses multicast technology, which uses Python to send multicast packets when verifying its security features, and to make a record here.

Python has complete support for sockets. Some of the APIs though have a different signature than the POSIX equivalents. It is therefore possible to write multicast servers and clients in Python. multicasting is the ability to send data to select set of hosts. Broadcasting in a network was to send data to all the hosts, unicast was when data is transfered between the hosts (typical One-one communication). Multicasting is the ability to send data to a multicast address and the clients are provided the data. Now so an introduction to multicasting are complete, lets look at how to write a simple multicast server and a multicast Client.

Multicast servers is written very similar to a standard unicast server, it the clients who has a slightly different cod E. The major change was made in setting socket options on the socket transmitting and receiving data.

The server code looks like this:
import socket
import time

any = "0.0.0.0″
senderport=1501
MCAST_ADDR = "224.168.2.9″
Mcast_port = 1600

Sock = Socket.socket (socket.af_inet, socket. SOCK_DGRAM, Socket. IPPROTO_UDP)
#The Sender is bound on (0.0.0.0:1501)
Sock.bind ((Any,senderport))
#Tell the kernel, we want to multicast and, the data is sent
#to Everyone (255 is the level of multicasting)
Sock.setsockopt (socket. IPPROTO_IP, Socket. Ip_multicast_ttl, 255)
While 1:
Time.sleep (10)
#send the data "Hello, World" to the multicast Addr:port
#Any subscribers to the multicast address would receive this data
Sock.sendto ("Hello World", (Mcast_addr,mcast_port));

The TTL field of 255 indicates that the data are unrestricted in scope.

The corresponding client code to receive the data being transmitted by the sender would look like this:
import socket
import time

any = "0.0.0.0″
mcast_addr =" 224.168.2.9″
Mcast_port = #create a UDP socket
sock = Socket.socket ( Socket.af_inet, Socket. SOCK_DGRAM, Socket. IPPROTO_UDP)
#allow multiple sockets to use the same PORT number
sock.setsockopt (socket. Sol_socket,socket. so_reuseaddr,1)
#Bind to the port, we know'll receive multicast Data
Sock.bind ((any,mcast_port))
#tell t He kernel that we are a multicast socket
sock.setsockopt (socket. IPPROTO_IP, Socket. Ip_multicast_ttl, 255)
#Tell The kernel, we want to add ourselves to a multicast group
#The address for the MU Lticast Group is the third param
status = sock.setsockopt (socket. IPPROTO_IP,
Socket. Ip_add_membership,
Socket.inet_aton (mcast_addr) + Socket.inet_aton (any));

Sock.setblocking (0)
ts = Time.time ()
While 1:
Try
data, addr = Sock.recvfrom (1024)
Except Socket.error, E:
Pass
Else
Print "We got data!"
Print "From:", addr
Print "Data:", data

The import part of the client code was the setsockopt for multicast sockets and to add the socket to a multicast group (sock Et. ip_add_membership). Also Note that the socket was bound to any:mcast_port. The server sends data to the multicast address and on the client side, the socket that's bound to the multicast port subs Cribes to the multicast group and receives data on the Mcast_port. It is important the client code bind itself to the Mcast_port to receive data from the multicast address. Another important thing to note are the mcast_addr. This address should is a valid multicast address, and not a unicast (Localhost/inaddr_any/any valid IP).

from:http://chaos.weblogs.us/archives/164

Python enables the sending and receiving of multicast data

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.