Search code examples
pythonsocketspython-idle

python server actively refuses connection


I am trying to create a simple client-server python socket program from example code located at this site. The only substantive changes I made were updating print() functions. I am getting a puzzling ConnectionRefusedError [WinError 10061] and cannot figure out why in such a simple example:

Client Code:

import socket
import sys
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_address = ('127.0.0.1', 12345)
print('client connecting to %s port %s' % server_address)
sock.connect(server_address)
try:
    message = 'This is the message.  It will be repeated.'
    print('sending "%s"' % message)
    sock.sendall(message.encode('utf-8'))
    amount_received = 0
    amount_expected = len(message.encode('utf-8'))
    while amount_received < amount_expected:
        data = sock.recv(16)
        amount_received += len(data)
        print('received "%s"' % data)
finally:
    print('closing socket')
    sock.close()

Server Code:

import socket
import sys
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_address = ('127.0.0.1', 12345)
print ('server starting up on %s port %s' % server_address)
sock.bind(server_address)
sock.listen(1)
while True:
    print('waiting for a connection')
    connection, client_address = sock.accept()
    try:
        print('connection from', client_address)
        while True:
            data = connection.recv(16)
            print('received "%s"' % data)
            if data:
                print('sending data back to the client')
                connection.sendall(data)
            else:
                print('no more data from', client_address)
                break
    finally:
        connection.close()

Expected Result:

(SERVER should show)
starting up on localhost port 12345
waiting for a connection
connection from ('127.0.0.1', *someaddress*)
received "This is the mess"
sending data back to the client
received "age.  It will be"
sending data back to the client
received " repeated."
sending data back to the client
received ""
no more data from ('127.0.0.1', *someaddress*)
waiting for a connection
(CLIENT should show)
connecting to localhost port 12345
sending "This is the message.  It will be repeated."
received "This is the mess"
received "age.  It will be"
received " repeated."
closing socket

Actual results from IDLE running Python 3.6.1:

Python 3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 18:41:36) [MSC v.1900 64 bit 
(AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> 
================ RESTART: C:\Users\____\Desktop\test01.py ================
server starting up on 127.0.0.1 port 12345
waiting for a connection

================ RESTART: C:/Users/____/Desktop/test02.py ==============
client connecting to 127.0.0.1 port 12345
Traceback (most recent call last):
  File "C:/Users/_____/Desktop/test02.py", line 6, in <module>
    sock.connect(server_address)
ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it

netstat status

I was curious to see if the process was listening so elevated my privileges and ran netstat -nab -- it shows my python server process listening on port 12345:

TCP    127.0.0.1:12345        0.0.0.0:0              LISTENING
[pythonw.exe]

The error message of "the target machine actively refused it" makes no sense to me as the server is apparently listening. Can anyone suggest what might be wrong in this simple example? Windows 10, Python 3.6.1 running in two separate IDLE windows. I have turned off all functions of Windows firewall.


Solution

  • I found the problem. I had to elevate my privileges on the instance of IDLE running the server -- and only the server -- by selecting "Run as Administrator" when launching that IDLE instance. That also had the effect that the resulting IDLE output shell was separate from the shell showing the output from the client.