socket.error:[errno 99] 无法在 python 中分配请求的地址和命名空间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19246103/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 13:14:33  来源:igfitidea点击:

socket.error:[errno 99] cannot assign requested address and namespace in python

pythonsocketsnamespacesip

提问by user2833462

My server software says errno99: cannot assign requested addresswhile using an ip address other than 127.0.0.1for binding.

我的服务器软件errno99: cannot assign requested address在使用除127.0.0.1绑定之外的 IP 地址时说。

But if the IP address is 127.0.0.1it works. Is it related to namespaces?

但如果IP地址是127.0.0.1它的工作。它与命名空间有关吗?

I am executing my server and client codes in another python program by calling execfile(). I am actually editing the mininet source code.I edited net.py and inside that I used execfile('server.py') execfile('client1.py') and execfile('client2.py').So as soon as "sudo mn --topo single,3" is called along with the creation of 3 hosts my server and client codes will get executed.I have given my server and client codes below.

我正在另一个 python 程序中通过调用execfile(). 我实际上正在编辑 mininet 源代码。我编辑了 net.py 并在其中使用了 execfile('server.py') execfile('client1.py') 和 execfile('client2.py')。所以只要“ sudo mn --topo single,3" 与 3 个主机的创建一起被调用,我的服务器和客户端代码将被执行。我在下面给出了我的服务器和客户端代码。

#server code
import select 
import socket 
import sys 
backlog = 5 
size = 1024 
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
server.bind(("10.0.0.1",9999)) 
server.listen(backlog) 
input = [server] 
running = 1 
while running: 
    inputready,outputready,exceptready = select.select(input,[],[]) 
    for s in inputready: 
        if s == server: 
            client, address = server.accept() 
            input.append(client)
        else: 
            l = s.recv(1024)
            sys.stdout.write(l)
server.close()



#client code
import socket
import select
import sys
import time
while(1) :
    s,addr=server1.accept()    
    data=int(s.recv(4))
    s = socket.socket()
    s.connect(("10.0.0.1",9999))
    while (1):
        f=open ("hello1.txt", "rb")
        l = f.read(1024)
        s.send(l)
        l = f.read(1024)
        time.sleep(5)
s.close()

采纳答案by Torxed

Stripping things down to basics this is what you would want to test with:

将事情分解为基础,这就是您想要测试的内容:

import socket
server = socket.socket() 
server.bind(("10.0.0.1", 6677)) 
server.listen(4) 
client_socket, client_address = server.accept()
print(client_address, "has connected")
while 1==1:
    recvieved_data = client_socket.recv(1024)
    print(recvieved_data)

This works assuming a few things:

假设以下几件事情是可行的:

  1. Your local IP address (on the server) is 10.0.0.1 (This video shows you how)
  2. No other software is listening on port 6677
  1. 您的本地 IP 地址(在服务器上)是 10.0.0.1(此视频向您展示如何
  2. 没有其他软件正在侦听端口 6677

Also note the basic concept of IP addresses:

还要注意 IP 地址的基本概念:

Try the following, open the start menu, in the "search" field type cmdand press enter. Once the black console opens up type ping www.google.comand this should give you and IP address for google. This address is googles local IP and they bind to that and obviously you can notbind to an IP address owned by google.

尝试以下操作,打开开始菜单,在“搜索”字段中键入cmd并按回车键。一旦黑色控制台打开类型ping www.google.com,这应该会给你和谷歌的IP地址。这个地址是谷歌的本地 IP,他们绑定到那个地址,显然你不能绑定到谷歌拥有的 IP 地址。

With that in mind, you own your own set of IP addresses. First you have the local IP of the server, but then you have the local IP of your house. In the below picture 192.168.1.50is the local IP of the server which you can bind to. You still own 83.55.102.40but the problem is that it's owned by the Router and not your server. So even if you visit http://whatsmyip.comand that tells you that your IP is 83.55.102.40that is not the case because it can only see where you're coming from.. and you're accessing your internet from a router.

考虑到这一点,您拥有自己的一组 IP 地址。首先你有服务器的本地IP,然后你有你家的本地IP。下图192.168.1.50是你可以绑定的服务器的本地IP。你仍然拥有,83.55.102.40但问题是它归路由器所有,而不是你的服务器。因此,即使您访问http://whatsmyip.com并告诉您您的 IP83.55.102.40并非如此,因为它只能看到您来自哪里......并且您正在从路由器访问您的互联网。

enter image description here

在此处输入图片说明

In order for your friends to access your server (which is bound to 192.168.1.50) you need to forward port 6677to 192.168.1.50and this is done in your router. Assuming you are behind one.

为了让您的朋友访问您的服务器(绑定到192.168.1.50),您需要将端口转发6677192.168.1.50,这是在您的路由器中完成的。假设你落后于一个。

If you're in school there's other dilemmas and routers in the way most likely.

如果您在学校,则很可能会遇到其他困境和路由器。

回答by WindyYang

Try like this: server.bind(("0.0.0.0", 6677))

试试这样: server.bind(("0.0.0.0", 6677))

回答by pumpkindle

when you bind localhostor 127.0.0.1, it means you can onlyconnect to your service from local.

当您绑定localhost或 时127.0.0.1,这意味着您只能从本地连接到您的服务。

you cannot bind 10.0.0.1because it not belong to you, you can only bind ip owned by your computer

你不能绑定,10.0.0.1因为它不属于你,你只能绑定你电脑拥有的ip

you can bind 0.0.0.0because it means all ip on your computer, so any ip can connect to your service if they can connect to any of your ip

您可以绑定,0.0.0.0因为这意味着您计算机上的所有 ip,因此任何 ip 都可以连接到您的服务,如果它们可以连接到您的任何 ip