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

use*_*462 24 python sockets ip namespaces

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

但如果IP地址是127.0.0.1有效的.它与命名空间有关吗?

我通过调用在另一个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()
Run Code Online (Sandbox Code Playgroud)


#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()
Run Code Online (Sandbox Code Playgroud)

Tor*_*xed 29

将事情简化为基础知识这是您想要测试的内容:

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)
Run Code Online (Sandbox Code Playgroud)

这可以假设一些事情:

  1. 您的本地IP地址(在服务器上)是10.0.0.1(此视频向您展示如何)
  2. No other software is listening on port 6677

Also note the basic concept of IP addresses:

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

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.50 is the local IP of the server which you can bind to. You still own 83.55.102.40 but the problem is that it's owned by the Router and not your server. So even if you visit http://whatsmyip.com and that tells you that your IP is 83.55.102.40 that is not the case because it can only see where you're coming from.. and you're accessing your internet from a router.

在此输入图像描述

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

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

  • @GabrielFair不确定,因为我不使用云服务.我总是使用自托管服务器,我可以控制硬件:)最简单的解决方法是在这种情况下执行`server.bind(("",6677))`因为这将绑定到端口上的所有可用接口6677 - 而不是将自己局限于已知的IP.我知道AWS通常会在外部更改IP,但在内部它们应该保持不变.您还可以使用以下方法之一:/sf/ask/18952181/ (3认同)
  • 谢谢你解释这个。如果您使用的是云服务器,这会如何变化。可以从 AWS 说吗? (2认同)

Voy*_*Voy 19

如果您尝试从 Docker 容器内连接到暴露的端口,而没有任何活动为该端口提供服务时,也会出现此错误。

在没有任何内容侦听/绑定到该端口的主机上,No connection could be made because the target machine actively refused it当向未提供服务的本地 URL 发出请求时,您会收到错误消息,例如:localhost:5000. 但是,如果您启动一个绑定到该端口的容器,但在其中运行的没有服务器实际为该端口提供服务,则对 localhost 上该端口的任何请求都将导致:

  • [Errno 99] Cannot assign requested address (如果从容器内调用),或
  • [Errno 0] Error (如果从容器外部调用)。

您可以按如下方式重现此错误和上述行为:

启动一个虚拟容器(注意:如果在本地找不到,这将拉取 python 图像):

docker run --name serv1 -p 5000:5000 -dit python
Run Code Online (Sandbox Code Playgroud)

然后[Errno 0] Error在主机上输入 Python 控制台,同时[Errno 99] Cannot assign requested address通过调用访问容器上的 Python 控制台:

docker exec -it -u 0 serv1 python
Run Code Online (Sandbox Code Playgroud)

然后在任何一种情况下调用:

docker run --name serv1 -p 5000:5000 -dit python
Run Code Online (Sandbox Code Playgroud)

我最后将这些错误中的任何一个视为等同于No connection could be made because the target machine actively refused it而不是试图解决它们的原因 - 尽管请告知这是否是一个坏主意。


鉴于我可以[Errno 99] Cannot assign requested address在绑定到被占用端口、连接到无效 IP、sysctl冲突、docker 网络问题、TIME_WAIT不正确以及还有很多事情。因此,我想把这个答案留在这里,尽管它不是手头问题的直接答案,因为它可能是这个问题中描述的错误的常见原因。

  • 挖得好!谢谢你!2020年依然会发生 (5认同)

小智 9

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

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

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


小智 5

像这样尝试:server.bind((“ 0.0.0.0”,6677))