为什么80%的码农都做不了架构师?>>>   

FROM:http://chaos.weblogs.us/archives/164
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 is to send data to all the hosts, unicast is when data is transfered between two hosts (typical one-one communication). Multicasting is the ability to send data to a multicast address and the clients are provided the data. Now that an introduction to multicasting is complete, lets look at how to write a simple multicast server and a multicast client.
Multicast servers are written very similar to a standard unicast server, its the clients who have a slightly different code. The major change is made in setting socket options on the socket transmitting and receiving data.

The server code looks like this:
################################################################
import socket, 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 that we want to multicast and that 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 will receive this data
    sock.sendto("Hello World", (MCAST_ADDR,MCAST_PORT));
################################################################
The TTL field of 255 indicates that the data is unrestricted in scope.

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

ANY = "0.0.0.0"
MCAST_ADDR = "224.168.2.9"
MCAST_PORT = 1600
#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 that we know will receive multicast data
sock.bind((ANY,MCAST_PORT))
#tell the kernel that we are a multicast socket
sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 255)
#Tell the kernel that we want to add ourselves to a multicast group
#The address for the multicast 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 is the setsockopt for multicast socket and to add the socket to a multicast group (socket.IP_ADD_MEMBERSHIP). Also note that the socket is bound to ANY:MCAST_PORT. The server sends data to the multicast address and on the client side, the socket that is bound to the multicast port subscribes 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 is the MCAST_ADDR. This address should be a valid multicast address, and not a unicast (localhost / INADDR_ANY / any valid IP).

TTL     Scope
----------------------------------------------------------------------
   0    Restricted to the same host. Won't be output by any interface.
1    Restricted to the same subnet. Won't be forwarded by a router. <If not otherwise specified, multicast datagrams are sent with a default value of 1, to prevent them to be forwarded beyond the local network.
 <32         Restricted to the same site, organization or department.
 <64 Restricted to the same region.
<128 Restricted to the same continent.
<255 Unrestricted in scope. Global.

224.0.1.0到238.255.255.255为用户可用的组播地址,在全网范围内有效。其中232.0.0.0/8为SSM组地址,而其余则属于ASM组地址。

http://www.h3c.com.cn/Products___Technology/Technology/Group_Management/Other_technology/Technology_book/200803/336048_30003_0.htm
根据接收者对组播源处理方式的不同,组播模型分为以下两大类:
ASM模型:即任意源组播模型。在ASM模型中,任一发送者都可作为组播源向某组播组地址发送组播信息,接收者通过加入由该组播组地址标识的组播组以获得发往该组播组的组播信息。在ASM模型中,接收者无法预先知道组播源的位置,但可以在任意时间加入或离开组播组。SSM模型:即指定信源组播模型。在现实生活中,用户可能只对某些组播源发送的组播信息感兴趣,而不愿接收其它源发送的信息。SSM模型为用户提供了一种能够在客户端指定组播源的传输服务。

转载于:https://my.oschina.net/kuafu/blog/37802

转-Multicast server and client in Python相关推荐

  1. java thrift client_使用thrift的java client调用python server

    上面这篇文章的例子是使用java client调用python server中的helloString方法来打印client传输过去的字符串 thrift文件,hello.thrift service ...

  2. 玩转Spring Cloud之配置中心(config server config client)

    玩转Spring Cloud之配置中心(config server &config client)  本文内容导航: 一.搭建配置服务中心(config server) 1.1.git方式 1 ...

  3. Python3 websocket server与client

    pip install websocket-client 此文主要用python3 client端 这个上传图片在linux下不行,传图片要用flask_server server: #!/usr/b ...

  4. firefox+linux+nginx搭建server与client通过证书双向认证环境

    项目中需要搭建一个server和client基于证书的双向认证环境.由我来做,我也不会. 经过一晚上的研究,基本摸清了(知其然不知其所以然).做下笔记. 基本环境: 1.安装nginx. 2.安装op ...

  5. Load balancer does not have available server for client

    最近在研究spring-cloud,研究zuul组件时发生下列错误:  Caused by: com.netflix.client.ClientException: Load balancer doe ...

  6. JVM Server与Client运行模式

    为什么80%的码农都做不了架构师?>>>    JVM Server模式与client模式启动,最主要的差别在于:-Server模式启动时,速度较慢,但是一旦运行起来后,性能将会有很 ...

  7. SQL Server native client与sqlcmd单独安装

    背景 我有一台虚拟机,想要连接sql server,但是又不想安装sql server怎么办. 方案 sql server有专门的访问客户端叫做sql server native client,然后还 ...

  8. mysql client dev_ubuntu下mysql安装(server、client、dev),开启、停止和重启,及常见错误...

    1. 在ubuntu下安装server和client很简单: (1)安装server apt-get install mysql-server 安装当中,会提示输入root账户的密码,按提示输入即可. ...

  9. python中引入sql的优点_SQL Server 2017中的Python:增强的数据库内机器学习

    Microsoft SQL Server是一款优秀的关系型数据库管理系统,Python是目前流行的数据科学语言之一,拥有丰富的库生态系统.从SQL Server 2017的CTP 2.0版本开始,可以 ...

最新文章

  1. [从菜鸟到高手演变]之智力题【史上最全】 (转)
  2. 深入浅出的“深拷贝与浅拷贝”
  3. 阿里淘宝一直在推的响应式编程到底是个什么鬼?
  4. RabbitMQ的Work模式
  5. opencv清晰度,色偏等评价函数
  6. MDSF:LOP-使用MPS来做个计算器的示例
  7. java处理XSS过滤的方法
  8. Linux系统安装Apache 2.4.6
  9. .NET 框架兼容性简介
  10. 计算机网络自顶向下方法实验报告,计算机网络自顶向下方法试验三报告.doc
  11. 「PKUWC2018」Slay the Spire
  12. ARM中断分析之一:中断控制器和CPU、外设的关系
  13. 真格量化-隐含波动率购买
  14. flask-sqlalchemy分表解决方案
  15. 牛客 处女座和小姐姐
  16. 机电传动与控制【2】
  17. Linux学习笔记(一):Linux常用命令
  18. uniapp 多国语言实现
  19. web 实时刷新 websocket 大数据
  20. java 模板 word转pdf 可分页 带图片

热门文章

  1. 2019牛客暑期多校训练营(第七场)D Number(思维)
  2. 第二章节 怀疑的练习和不会的练习
  3. 【机器学习实战】Machine Learning in Action 代码 视频 项目案例
  4. 读书笔记九:TCP/IP详解之广播和多播,IGMP协议
  5. OC语言Block和协议
  6. MySql like 查询 变向写法(不用like 完成like查询)
  7. Mysql的一些问题
  8. 2018年广东工业大学文远知行杯新生程序设计竞赛 1004 明日会吹明日的风β
  9. 团队-象棋游戏-需求分析
  10. C++学习基础三——迭代器基础