2016-08-15 111 views
1

我按照名为“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() 

,这是我的错误:

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 
>>> 
+0

检查'bind_ip'以查看它是否是使用'ifconfig'(linux)或'ipconfig'(windows)绑定的正确ip。尝试使用'''''bind_ip'绑定到任何接口。另外,请不要**使用端口21 - 它被保留用于FTP。使用从1024到49151的任意数字。 –

+0

您的IP地址是本地还是远程?如果是远程,请使用.connect()方法 –

回答

2

您试图绑定到实际未分配的IP地址您的网络接口:

bind_ip = "184.168.237.1" 

Windows Sockets Error Codes documentation

WSAEADDRNOTAVAIL 10049
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.

这可能是您的路由器在使用NAT(网络地址转换)与您的计算机交谈之前正在监听的IP地址,但这并不意味着您的计算机完全可以看到该IP地址。

绑定至0.0.0.0,将使用所有可用的IP地址(本地主机都配置及任何公开的地址):

bind_ip = "0.0.0.0" 

或使用您的计算机配置为任何地址;在控制台中运行ipconfig /all以查看您的网络配置。

您可能也不想使用端口< 1024;这些保留给只作为根用户运行的进程。你必须挑选比一个较大的数字,如果你想运行一个非特权进程(在大多数教程项目,这正是你想要的):

port = 5021 # arbitrary port number higher than 1023 

我相信具体的教程你以下使用BIND_IP = '0.0.0.0'BIND_PORT = 9090

相关问题