Why am I getting the error "connection refused" in Python? (Sockets)

PythonSockets

Python Problem Overview


I'm new to Sockets, please excuse my complete lack of understanding.

I have a server script(server.py):

#!/usr/bin/python

import socket #import the socket module

s = socket.socket() #Create a socket object
host = socket.gethostname() #Get the local machine name
port = 12397 # Reserve a port for your service
s.bind((host,port)) #Bind to the port

s.listen(5) #Wait for the client connection
while True:
	c,addr = s.accept() #Establish a connection with the client
	print "Got connection from", addr
	c.send("Thank you for connecting!")
	c.close()

and client script (client.py):

#!/usr/bin/python 

import socket #import socket module

s = socket.socket() #create a socket object
host = '192.168.1.94' #Host i.p
port = 12397 #Reserve a port for your service

s.connect((host,port))
print s.recv(1024)
s.close

I go to my desktop terminal and start the script by typing:

python server.py

after which, I go to my laptop terminal and start the client script:

python client.py

but I get the following error:

> File "client.py", line 9, in > > s.connect((host,port))
> >File "/usr/lib/python2.7/socket.py", line 224, in meth > > return getattr(self._sock,name)(*args) > > socket.error: [Errno 111] Connection refused

I've tried using different port numbers to no avail. However, I was able to get the host name using the same ip and the gethostname() method in the client script and I can ping the desktop (server).

Python Solutions


Solution 1 - Python

Instead of

host = socket.gethostname() #Get the local machine name
port = 12397 # Reserve a port for your service
s.bind((host,port)) #Bind to the port

you should try

port = 12397 # Reserve a port for your service
s.bind(('', port)) #Bind to the port

so that the listening socket isn't too restricted. Maybe otherwise the listening only occurs on one interface which, in turn, isn't related with the local network.

One example could be that it only listens to 127.0.0.1, which makes connecting from a different host impossible.

Solution 2 - Python

This error means that for whatever reason the client cannot connect to the port on the computer running server script. This can be caused by few things, like lack of routing to the destination, but since you can ping the server, it should not be the case. The other reason might be that you have a firewall somewhere between your client and the server - it could be on server itself or on the client. Given your network addressing, I assume both server and client are on the same LAN, so there shouldn't be any router/firewall involved that could block the traffic. In this case, I'd try the following:

  • check if you really have that port listening on the server (this should tell you if your code does what you think it should): based on your OS, but on linux you could do something like netstat -ntulp
  • check from the server, if you're accepting the connections to the server: again based on your OS, but telnet LISTENING_IP LISTENING_PORT should do the job
  • check if you can access the port of the server from the client, but not using the code: just us the telnet (or appropriate command for your OS) from the client

and then let us know the findings.

Solution 3 - Python

Assume s = socket.socket() The server can be bound by following methods: Method 1:

host = socket.gethostname()
s.bind((host, port))

Method 2:

host = socket.gethostbyname("localhost")  #Note the extra letters "by"
s.bind((host, port))

Method 3:

host = socket.gethostbyname("192.168.1.48")
s.bind((host, port))

If you do not exactly use same method on the client side, you will get the error: socket.error errno 111 connection refused.

So, you have to use on the client side exactly same method to get the host, as you do on the server. For example, in case of client, you will correspondingly use following methods:

Method 1:

host = socket.gethostname() 
s.connect((host, port))

Method 2:

host = socket.gethostbyname("localhost") # Get local machine name
s.connect((host, port))

Method 3:

host = socket.gethostbyname("192.168.1.48") # Get local machine name
s.connect((host, port))

Hope that resolves the problem.

Solution 4 - Python

host = socket.gethostname()  # Get the local machine name
port = 12397                 # Reserve a port for your service
s.bind((host,port))          # Bind to the port

I think this error may related to the DNS resolution. This sentence host = socket.gethostname() get the host name, but if the operating system can not resolve the host name to local address, you would get the error. Linux operating system can modify the /etc/hosts file, add one line in it. It looks like below( 'hostname' is which socket.gethostname() got).

127.0.0.1   hostname

Solution 5 - Python

in your server.py file make : host ='192.168.1.94' instead of host = socket.gethostname()

Solution 6 - Python

Pay attention to change the port number. Sometimes, you need just to change the port number. I experienced that when i made changes over changes over syntax and functions.

Solution 7 - Python

I was being able to ping my connection but was STILL getting the 'connection refused' error. Turns out I was pinging myself! That's what the problem was.

Solution 8 - Python

I was getting the same problem in my code, and after thow days of search i finally found the solution, and the problem is the function socket.gethostbyname(socket.gethostname) doesnt work in linux so instead of that you have to use socket.gethostbyname('put the hostname manually') not socket.gethostbyname('localhost'), use socket.gethostbyname('host') looking with ifconfig.

Solution 9 - Python

try this command in terminal:

sudo ufw enable
ufw allow 12397

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSheldonView Question on Stackoverflow
Solution 1 - PythonglglglView Answer on Stackoverflow
Solution 2 - PythonSpankMeView Answer on Stackoverflow
Solution 3 - PythonM Shiraz BaigView Answer on Stackoverflow
Solution 4 - PythonaoniaoView Answer on Stackoverflow
Solution 5 - PythonMajd SaadaouiView Answer on Stackoverflow
Solution 6 - PythonAndreiView Answer on Stackoverflow
Solution 7 - PythonazizbroView Answer on Stackoverflow
Solution 8 - PythonJuander ContrerasView Answer on Stackoverflow
Solution 9 - Pythonmou ayadView Answer on Stackoverflow