Errno 10061 : No connection could be made because the target machine actively refused it ( client - server )

PythonSockets

Python Problem Overview


I have a problem with these client and server codes, I keep getting the [Errno 10061] No connection could be made because the target machine actively refused it

I'm running the server on a virtual machine with Windows XP SP3 and the client on Windows 7 64bit, my python version is 2.7.3. What I want to know is how should I edit the code to use the client and server on different networks! Thanks!

server :

#!/usr/bin/python           # This is server.py file

import socket               # Import socket module
s = socket.socket()         # Create a socket object
host = '0.0.0.0' # Get local machine name
port = 12345                # Reserve a port for your service.


print 'Server started!'
print 'Waiting for clients...'

s.bind((host, port))        # Bind to the port
s.listen(5)                 # Now wait for client connection.
c, addr = s.accept()     # Establish connection with client.
print 'Got connection from', addr
while True:
  msg = c.recv(1024)
  print addr, ' >> ', msg
  msg = raw_input('SERVER >> ')
  c.send(msg);
  #c.close()                # Close the connection

client :

#!/usr/bin/python           # This is client.py file

import socket               # Import socket module

s = socket.socket()         # Create a socket object
host = socket.gethostname() # Get local machine name
port = 12345                # Reserve a port for your service.

print 'Connecting to ', host, port
s.connect((host, port))

while True:
  msg = raw_input('CLIENT >> ')
  s.send(msg)
  msg = s.recv(1024)
  print 'SERVER >> ', msg
#s.close                     # Close the socket when done

PS : code is from internet.

Python Solutions


Solution 1 - Python

10061 is WSAECONNREFUSED, 'connection refused', which means there was nothing listening at the IP:port you tried to connect to.

There was a firewall product around the year 2000 that issued refusals instead of ignoring incoming connections to blocked ports, but this was quickly recognised as an information leak to attackers and corrected or withdrawn.

Solution 2 - Python

Hint: actively refused sounds like somewhat deeper technical trouble, but...

...actually, this response (and also specifically errno:10061) is also given, if one calls the bin/mongo executable and the mongodb service is simply not running on the target machine. This even applies to local machine instances (all happening on localhost).

Always rule out for this trivial possibility first, i.e. simply by using the command line client to access your db.

See here.

Solution 3 - Python

Using the examples from: https://docs.python.org/3.2/library/socketserver.html I determined that I needed to set the HOST port to the machine I had the server program running on. So TCPServer on 192.168.0.1 HOST = TCPServer IP 192.168.0.1 then I had to set the TCPClient side to point to the TCPServer IP. So the TCPClient HOST value = 192.168.0.1 - Sorry, that's the best I can describe it.

Solution 4 - Python

So I was facing the same issue, and the solution that worked for me was...

I am assuming your server and client program are written in python.

  1. First, open one python shell
  2. open and run the Server program first
  3. then open another different python shell
  4. open and run the Client program here
  5. done !!

Solution 5 - Python

There is no relationship between error and firewall.

first, run server program,
then run client program in another shell of python

and it will work

Solution 6 - Python

instead of localhost of '0.0.0.0', use local network address as host in case of both - the server and the client - code.

host = '192.168.12.12' port = 12345

use this host address when binding and connecting to the socket.

server.bind((host, port)) client.connect((host, port))

this change solved the issue for me.

Solution 7 - Python

The solution is to use the same IP and Port number in both client and server. Try, in client to use TCP_IP = 'write the ip number here' TCP_PORT = writ the port number here s.connect((TCP_IP, TCP_PORT))

Solution 8 - Python

if you have remote server installed on you machine. give server.py host as "localhost" and the port number. then client side , you have to give local ip- 127.0.0.1 and port number. then its works

Solution 9 - Python

I was facing a similar problem when I was calling REST API using python library and what I found that my server was going into sleep mode which was leading to this. As soon as I logged in to the server via Remote Desktop Connection, my API call used to work.

Solution 10 - Python

This could be because of proxy or firewall. If it's proxy, then you need to specify proxy setting at entry point of your code or project.

import os #for proxy

proxy = 'http://10.XX.XX.XX:8X8X' #your own proxy 'http://<user>:<pass>@<proxy>:<port>'

os.environ['http_proxy'] = proxy 
os.environ['HTTP_PROXY'] = proxy
os.environ['https_proxy'] = proxy
os.environ['HTTPS_PROXY'] = proxy
#rest of code .....

Solution 11 - Python

The below changes fixed my problem. I struggled with the same error for a week. I would like to share with you all that the solution is simply host = '' in the server and the client host = ip of the server.  

Solution 12 - Python

The first: Please make sure your port '12345' is opening and then when you using a different network. You have to use the IP address in LAN. Don't use the 'localhost' or '127.0.0.1'. The solution here is: In server

host = '192.168.1.12' #Ip address in LAN network

In client

host = '27.32.123.32' # external IP Address

Hope it works for you

Solution 13 - Python

I had errors 10060 and 10061. The reason was in my antivirus(Eset Nod 32). Try to turn off the Firewall of your antivirus as I did or just delete it for a time to test the program. If everything started to work properly, add that program to the exclusion or switch to another antivirus. Also, try to change the 'host' variable to an empty string:

host = ''

And add socket.AF_INET, socket.SOCK_STREAM to the 's' variable:

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

Solution 14 - Python

I was doing this tutorial and they said that windows users will have a problem. They said that you can check the Windows Firewall to fix the problem. Let me show you a quick Google Search on how to change the windows firewall:

Go to Start and open Control Panel. Select System and Security > Windows Defender Firewall. Choose Turn Windows Firewall on or off. Select Turn on Windows Firewall for domain, private, and public network settings.

After that, your app should work. Also, in your client(not server side) the port should be 0.0.0.0 and in the server side, it should be 127.0.0.1.

Solution 15 - Python

If the client and server are running on the same machine (as in running 2 programs), it's OK to config both IP address as locahost. However, if you are to run them on different machines (including VMs), you need to

  1. Make sure they are on the same subnet (usually by pinging each other)
  2. The Server needs to config the host IP as its IP address (like 192.168.xxx.xxx instead of localhost or 127.0.0.1). You may find the IP address by running ipconfig on Windows or ip a on Unix-like server

This change worked for me with my Client on Windows and Server on Ubuntu VM.

Solution 16 - Python

the short term solution is to use the default iis host and port normally 120.0.0.1 and 80 respectively. However am still looking for a more versatile solution.

Solution 17 - Python

When you run the code on windows machine, firewall prompts it to allow network access, allow the network access and it will work, if it does not prompts, go to firewall settings > allow an app through firewall and select your python.exe and allow network access.

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
QuestionhavoxView Question on Stackoverflow
Solution 1 - Pythonuser207421View Answer on Stackoverflow
Solution 2 - PythonFrank NockeView Answer on Stackoverflow
Solution 3 - Pythonpurza95View Answer on Stackoverflow
Solution 4 - Pythonrv_powerView Answer on Stackoverflow
Solution 5 - Pythonmohammd_dfView Answer on Stackoverflow
Solution 6 - PythonAditya LodhaView Answer on Stackoverflow
Solution 7 - PythonM.SelmyView Answer on Stackoverflow
Solution 8 - PythonZzmilanzz ZzmadubashazzView Answer on Stackoverflow
Solution 9 - PythonVinaykumar PatelView Answer on Stackoverflow
Solution 10 - PythonManjeetView Answer on Stackoverflow
Solution 11 - PythonLibrossView Answer on Stackoverflow
Solution 12 - PythonSang9xproView Answer on Stackoverflow
Solution 13 - PythonЕвгений ВолощукView Answer on Stackoverflow
Solution 14 - PythonMaryam Ibrahim StudentView Answer on Stackoverflow
Solution 15 - PythonHenry FungView Answer on Stackoverflow
Solution 16 - PythonKarisno1View Answer on Stackoverflow
Solution 17 - Pythononly4View Answer on Stackoverflow