socket.error:[errno 99] 无法在 python 中分配请求的地址和命名空间 [英] socket.error:[errno 99] cannot assign requested address and namespace in python

查看:33
本文介绍了socket.error:[errno 99] 无法在 python 中分配请求的地址和命名空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的服务器软件说errno99:在使用127.0.0.1以外的IP地址进行绑定时,无法分配请求的地址.

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

但是如果 IP 地址是 127.0.0.1 它可以工作.是否与命名空间有关?

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

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

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.

#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()

推荐答案

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

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)

这需要做一些事情:

  1. 您的本地 IP 地址(在服务器上)是 10.0.0.1(此视频向您展示如何)
  2. 没有其他软件在监听 6677 端口

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

尝试以下操作,打开开始菜单,在搜索"字段中键入 cmd 并按 Enter.黑色控制台打开后,键入 ping www.google.com,这应该会为您提供 google 的 IP 地址.这个地址是谷歌的本地 IP,他们绑定到那个地址,显然你可以绑定到谷歌拥有的 IP 地址.

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.

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

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.

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

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.

这篇关于socket.error:[errno 99] 无法在 python 中分配请求的地址和命名空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆