Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The requested address is not valid in its context error

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
>>> 
like image 585
MountainSide Studios Avatar asked Aug 15 '16 15:08

MountainSide Studios


1 Answers

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.

like image 89
Martijn Pieters Avatar answered Sep 19 '22 12:09

Martijn Pieters