2016-08-15 64 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

相關問題