无法分配请求的地址:Python 多播
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19798254/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can't assign requested address : Python Multicasting
提问by Kyuubi
I just started with networking and am writing a very simple code for multicasting. I am still not sure about the different interfaces. Some examples used "0.0.0.0" while others have used "127.0.0.1".
我刚开始使用网络,正在编写一个非常简单的多播代码。我仍然不确定不同的接口。一些示例使用“0.0.0.0”,而其他示例使用“127.0.0.1”。
Code for Server
服务器代码
import socket
import sys
import time
ANY = socket.gethostbyname('localhost')
S_PORT = 1501
M_ADDR = "224.168.2.9"
M_PORT = 1600
sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM,socket.IPPROTO_UDP)
sock.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEPORT,1)
sock.bind((ANY,S_PORT))
sock.setsockopt(socket.IPPROTO_IP,socket.IP_MULTICAST_TTL,255)
while 1:
message = raw_input("Enter message: ")
sock.sendto(message,(M_ADDR,M_PORT))
if message == "exit":
break
sock.close()
Code for Client
客户端代码
import socket
import time
import sys
ANY = socket.gethostbyname('localhost')
M_ADDR = "224.168.2.9"
M_PORT = 1600
sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM,socket.IPPROTO_UDP)
sock.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEPORT,1)
sock.bind((ANY,M_PORT))
sock.setsockopt(socket.IPPROTO_IP,socket.IP_MULTICAST_TTL,255)
status = sock.setsockopt(socket.IPPROTO_IP,socket.IP_ADD_MEMBERSHIP,socket.inet_aton(M_ADDR) + socket.inet_aton(ANY))
while 1:
data,addr = sock.recvfrom(1024)
print "Received message from " + str(addr) + " : " + data
if data == "exit":
break
sock.close()
The Client code runs properly and is waiting to receive message on the socket. But the Code Server crashes as soon as I enter any message.
客户端代码运行正常,正在等待接收套接字上的消息。但是只要我输入任何消息,代码服务器就会崩溃。
Traceback (most recent call last):
File "multicast_server.py", line 17, in <module>
sock.sendto(message,(M_ADDR,M_PORT))
socket.error: [Errno 49] Can't assign requested address
What is causing this issue ?
The above code works if I use ANY = "0.0.0.0"
. Why is that ? What changes ?
是什么导致了这个问题?如果我使用ANY = "0.0.0.0"
. 这是为什么 ?有什么变化?
采纳答案by abarnert
In IPv4, 0.0.0.0 is a special address, aka INADDR_ANY
, that means "bind every possible address on every interface".
在 IPv4 中, 0.0.0.0 是一个特殊地址,也就是INADDR_ANY
,这意味着“绑定每个接口上的每个可能的地址”。
So, the multicast network at 224.168.2.9, if it's reachable at all, will certainly be reachable from a socket bound to 0.0.0.0.
因此,在 224.168.2.9 的多播网络,如果它完全可以访问,肯定可以从绑定到 0.0.0.0 的套接字访问。
Meanwhile, 127.0.0.1 is a special address, aka INADDR_LOOPBACK
, that means "bind localhost only on the loopback device". There's no way to reach anything but the local host itself on that socket. In particular, you can't reach your multicast network. Whether you get an ENETUNREACH
, ENETDOWN
, or EADDRNOTAVAIL
is platform-specific, but whether it works is not—it can't possibly work.
同时, 127.0.0.1 是一个特殊地址,也就是INADDR_LOOPBACK
,这意味着“仅在环回设备上绑定本地主机”。除了该套接字上的本地主机本身之外,无法访问任何内容。特别是,您无法访问多播网络。无论您获得的是ENETUNREACH
、ENETDOWN
还是EADDRNOTAVAIL
特定于平台的,但它是否有效都不是——它不可能有效。
If you want to test multicasting without testing across multiple computers, you will need to set up a loopback network with more than one address, so you can bind the client, the server, and the multicast group all to different addresses within that network.
如果您想在不跨多台计算机测试的情况下测试多播,您将需要设置一个具有多个地址的环回网络,以便您可以将客户端、服务器和多播组都绑定到该网络中的不同地址。
回答by andrewgrz
When you use "0.0.0.0"
or ""
for networking in python, it opens up to any IP inbound. For your case, I would use "0.0.0.0"
or "127.0.0.1"
(if you are not comfortable opening up to the world.)
当您在 python 中使用"0.0.0.0"
或""
网络时,它会打开任何 IP 入站。对于您的情况,我会使用"0.0.0.0"
或"127.0.0.1"
(如果您不习惯向世界开放。)
回答by user6475064
get your device ip address ubuntu: ifcongfig
获取您的设备 IP 地址 ubuntu:ifconfig
choose the ip address from any ethernet ,loop, wlan replace M_ADDR with that ip address
从任何以太网,循环中选择 ip 地址,wlan 用该 ip 地址替换 M_ADDR