Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

socket.error:[errno 99] cannot assign requested address and namespace in python

My server software says errno99: cannot assign requested address while using an ip address other than 127.0.0.1 for binding.

But if the IP address is 127.0.0.1 it works. Is it related to namespaces?

I am executing my server and client codes in another python program by calling execfile(). I am actually editing the mininet source code.I edited net.py and inside that I used execfile('server.py') execfile('client1.py') and execfile('client2.py').So as soon as "sudo mn --topo single,3" is called along with the creation of 3 hosts my server and client codes will get executed.I have given my server and client codes below.

#server code import select  import socket  import sys  backlog = 5  size = 1024  server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)  server.bind(("10.0.0.1",9999))  server.listen(backlog)  input = [server]  running = 1  while running:      inputready,outputready,exceptready = select.select(input,[],[])      for s in inputready:          if s == server:              client, address = server.accept()              input.append(client)         else:              l = s.recv(1024)             sys.stdout.write(l) server.close() 


#client code import socket import select import sys import time while(1) :     s,addr=server1.accept()         data=int(s.recv(4))     s = socket.socket()     s.connect(("10.0.0.1",9999))     while (1):         f=open ("hello1.txt", "rb")         l = f.read(1024)         s.send(l)         l = f.read(1024)         time.sleep(5) s.close() 
like image 900
user2833462 Avatar asked Oct 08 '13 11:10

user2833462


People also ask

Could not connect Errno 99 Cannot assign requested address?

Docker container [Errno 99] Cannot assign requested address Note that for Docker containers, either you need to run them in network_mode: host to use the host's network systemd, or you need to bind to the container's IP address.

What (): Cannot assign requested address?

This error is not specifically for macOS, it tells you that the provided IP is not available for your network or it's already in use. You should provide an assigned IP from your networks, you could execute hostname -I (or equivalent) to check them.


2 Answers

Stripping things down to basics this is what you would want to test with:

import socket server = socket.socket()  server.bind(("10.0.0.1", 6677))  server.listen(4)  client_socket, client_address = server.accept() print(client_address, "has connected") while True:     recvieved_data = client_socket.recv(1024)     print(recvieved_data) 

This works assuming a few things:

  1. Your local IP address (on the server) is 10.0.0.1 (This video shows you how)
  2. No other software is listening on port 6677

Also note the basic concept of IP addresses:

Try the following, open the start menu, in the "search" field type cmd and press enter. Once the black console opens up type ping www.google.com and this should give you and IP address for google. This address is googles local IP and they bind to that and obviously you can not bind to an IP address owned by google.

With that in mind, you own your own set of IP addresses. First you have the local IP of the server, but then you have the local IP of your house. In the below picture 192.168.1.50 is the local IP of the server which you can bind to. You still own 83.55.102.40 but the problem is that it's owned by the Router and not your server. So even if you visit http://whatsmyip.com and that tells you that your IP is 83.55.102.40 that is not the case because it can only see where you're coming from.. and you're accessing your internet from a router.

enter image description here

In order for your friends to access your server (which is bound to 192.168.1.50) you need to forward port 6677 to 192.168.1.50 and this is done in your router. Assuming you are behind one.

If you're in school there's other dilemmas and routers in the way most likely.

like image 108
Torxed Avatar answered Oct 02 '22 10:10

Torxed


This error will also appear if you try to connect to an exposed port from within a Docker container, when nothing is actively serving the port.

On a host where nothing is listening/bound to that port you'd get a No connection could be made because the target machine actively refused it error instead when making a request to a local URL that is not served, eg: localhost:5000. However, if you start a container that binds to the port, but there is no server running inside of it actually serving the port, any requests to that port on localhost will result in:

  • [Errno 99] Cannot assign requested address (if called from within the container), or
  • [Errno 0] Error (if called from outside of the container).

You can reproduce this error and the behaviour described above as follows:

Start a dummy container (note: this will pull the python image if not found locally):

docker run --name serv1 -p 5000:5000 -dit python 

Then for [Errno 0] Error enter a Python console on host, while for [Errno 99] Cannot assign requested address access a Python console on the container by calling:

docker exec -it -u 0 serv1 python 

And then in either case call:

import urllib.request urllib.request.urlopen('https://localhost:5000') 

I concluded with treating either of these errors as equivalent to No connection could be made because the target machine actively refused it rather than trying to fix their cause - although please advise if that's a bad idea.


I've spent over a day figuring this one out, given that all resources and answers I could find on the [Errno 99] Cannot assign requested address point in the direction of binding to an occupied port, connecting to an invalid IP, sysctl conflicts, docker network issues, TIME_WAIT being incorrect, and many more things. Therefore I wanted to leave this answer here, despite not being a direct answer to the question at hand, given that it can be a common cause for the error described in this question.

like image 21
Voy Avatar answered Oct 02 '22 10:10

Voy