Python 请求的地址在其上下文错误中无效
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38958233/
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
The requested address is not valid in its context error
提问by MountainSide Studios
I was following a tutorial called "Black Hat Python" and got a "the requested address is not valid in its context" error. I'm Python IDE version: 2.7.12 This is my code:
我正在学习一个名为“Black Hat Python”的教程,并收到“请求的地址在其上下文中无效”错误。我是 Python IDE 版本:2.7.12 这是我的代码:
import socket
import threading
bind_ip = "184.168.237.1"
bind_port = 21
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind((bind_ip,bind_port))
server.listen(5)
print "[*] Listening on %s:%d" % (bind_ip,bind_port)
def handle_client(client_socket):
request = client_socket.rev(1024)
print "[*] Recieved: %s" % request
client_socket.close()
while True:
client,addr = server.accept()
print "[*] Accepted connection from: %s:%d" % (addr[0],addr[1])
client_handler = threading.Thread(target=handle_client,args=(client,))
client_handler.start()
and this is my error:
这是我的错误:
Traceback (most recent call last):
File "C:/Python34/learning hacking.py", line 9, in <module>
server.bind((bind_ip,bind_port))
File "C:\Python27\lib\socket.py", line 228, in meth
return getattr(self._sock,name)(*args)
error: [Errno 10049] The requested address is not valid in its context
>>>
回答by Martijn Pieters
You are trying to bind to an IP address that is not actually assigned to your network interface:
您正在尝试绑定到实际上并未分配给您的网络接口的 IP 地址:
bind_ip = "184.168.237.1"
See the Windows Sockets Error Codesdocumentation:
WSAEADDRNOTAVAIL10049
Cannot assign requested address.The requested address is not valid in its context. This normally results from an attempt to bind to an address that is not valid for the local computer.
WSAEADDRNOTAVAIL10049
无法分配请求的地址。请求的地址在其上下文中无效。这通常是由于尝试绑定到对本地计算机无效的地址造成的。
That may be an IP address that your router is listening to before using NAT (network address translation) to talk to your computer, but that doesn't mean your computer sees that IP address at all.
这可能是您的路由器在使用 NAT(网络地址转换)与您的计算机通信之前侦听的 IP 地址,但这并不意味着您的计算机根本无法看到该 IP 地址。
Either bind to 0.0.0.0
, which will use all available IP addresses (both localhost and any public addresses configured):
要么绑定到0.0.0.0
,这将使用所有可用的 IP 地址(本地主机和配置的任何公共地址):
bind_ip = "0.0.0.0"
or use any address that your computer is configured for; run ipconfig /all
in a console to see your network configuration.
或使用您的计算机配置的任何地址;ipconfig /all
在控制台中运行以查看您的网络配置。
You probably also don't want to use ports < 1024; those are reserved for processes running as root only. You'll have to pick a higher number than that if you want to run an unprivileged process (and in the majority of tutorials programs, that is exactly what you want):
您可能也不想使用小于 1024 的端口;这些是为仅以 root 身份运行的进程保留的。如果你想运行一个非特权进程,你必须选择一个比这个更高的数字(在大多数教程程序中,这正是你想要的):
port = 5021 # arbitrary port number higher than 1023
I believe the specific tutorial you are following uses BIND_IP = '0.0.0.0'
and BIND_PORT = 9090
.
我相信您正在关注的具体教程使用BIND_IP = '0.0.0.0'
和BIND_PORT = 9090
.