The requested address is not valid in its context error

扶醉桌前 提交于 2019-12-02 00:37:31

问题


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:

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

回答1:


You are trying to bind to an IP address that is not actually assigned to your network interface:

bind_ip = "184.168.237.1"

See the 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.

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.

Either bind to 0.0.0.0, which will use all available IP addresses (both localhost and any public addresses configured):

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.

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):

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.



来源:https://stackoverflow.com/questions/38958233/the-requested-address-is-not-valid-in-its-context-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!