套接字。错误:[errno 99]无法在python中分配请求的地址和名称空间

时间:2022-10-27 07:34:06

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

我的服务器软件说errno99:在使用127.0.0.1以外的ip地址进行绑定时不能分配请求的地址。

But if the IP address is 127.0.0.1 it 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.

我正在通过调用execfile()在另一个python程序中执行服务器和客户机代码。我正在编辑mininet源代码。我编辑网。在里面我使用了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()

1 个解决方案

#1


24  

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. 您的本地IP地址(在服务器上)为10.0.0.1(此视频向您展示如何)
  3. No other software is listening on port 6677
  4. 没有其他软件正在监听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.

尝试以下操作,在“搜索”字段类型cmd中打开“开始”菜单,然后按enter。一旦黑控制台打开,键入ping www.google.com,它将为谷歌提供您和IP地址。这个地址是google本地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.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.

考虑到这一点,您拥有自己的一组IP地址。首先你有服务器的本地IP,然后你有你房子的本地IP。在下图中,192.168.1.50是您可以绑定到的服务器的本地IP。您仍然拥有83.55.102.40,但问题是它属于路由器,而不是您的服务器。所以,即使你访问了http://whatsmyip.com,它告诉你你的IP是83.55.102.40,但事实并非如此,因为它只能看到你从哪里来。你可以通过路由器上网。

套接字。错误:[errno 99]无法在python中分配请求的地址和名称空间

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.

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

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

如果你在学校,你很可能会遇到其他的难题和路由器。

#1


24  

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. 您的本地IP地址(在服务器上)为10.0.0.1(此视频向您展示如何)
  3. No other software is listening on port 6677
  4. 没有其他软件正在监听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.

尝试以下操作,在“搜索”字段类型cmd中打开“开始”菜单,然后按enter。一旦黑控制台打开,键入ping www.google.com,它将为谷歌提供您和IP地址。这个地址是google本地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.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.

考虑到这一点,您拥有自己的一组IP地址。首先你有服务器的本地IP,然后你有你房子的本地IP。在下图中,192.168.1.50是您可以绑定到的服务器的本地IP。您仍然拥有83.55.102.40,但问题是它属于路由器,而不是您的服务器。所以,即使你访问了http://whatsmyip.com,它告诉你你的IP是83.55.102.40,但事实并非如此,因为它只能看到你从哪里来。你可以通过路由器上网。

套接字。错误:[errno 99]无法在python中分配请求的地址和名称空间

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.

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

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

如果你在学校,你很可能会遇到其他的难题和路由器。