【问题标题】:file transfer using udp multicast in python ?在 python 中使用 udp 多播进行文件传输?
【发布时间】:2014-04-11 18:25:05
【问题描述】:

如何在 Python 中使用 UDP 多播发送和接收文件, 是否有标准库可以做到这一点?或任何其他标准功能模块?

【问题讨论】:

    标签: python file multicast transfer


    【解决方案1】:

    Here's info on how to do just that 带有源示例:

    import socket
    import struct
    import sys
    
    message = 'very important data'
    multicast_group = ('224.3.29.71', 10000)
    
    # Create the datagram socket
    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    
    # Set a timeout so the socket does not block indefinitely when trying
    # to receive data.
    sock.settimeout(0.2)
    try:
    
        # Send data to the multicast group
        print >>sys.stderr, 'sending "%s"' % message
        sent = sock.sendto(message, multicast_group)
    
        # Look for responses from all recipients
        while True:
            print >>sys.stderr, 'waiting to receive'
            try:
                data, server = sock.recvfrom(16)
            except socket.timeout:
                print >>sys.stderr, 'timed out, no more responses'
                break
            else:
                print >>sys.stderr, 'received "%s" from %s' % (data, server)
    
    finally:
        print >>sys.stderr, 'closing socket'
        sock.close()
    

    【讨论】:

    • 如何发送和接收文件而不是消息
    • 这是一个单独的问题。 :) 打开文件,从中读取数据,然后将该数据作为消息发送。网上到处都是关于如何在 python 中进行文件处理的教程。这也只是示例代码。无论如何,您都必须使其适应您的使用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-10
    • 2015-03-06
    相关资源
    最近更新 更多