Checking refusal of a network connection

Cameron Simpson cs at
Fri May 31 19:17:38 EDT 2019

On 31May2019 17:35, Markus Elfring <Markus.Elfring at> wrote:
>I can start a service as desired.
>elfring at Sonne:~/Projekte/Bau/C++/test-statistic-server1/local> ./test-statistic-server2 & /usr/bin/ss -t -l -p -H|grep test
>[1] 8961
>waiting for connections
>server_id: localhost
>server_port: 35529
>LISTEN     0           123                  [::1]:35529              [::]:*      users:(("test-statistic-",pid=8961,fd=3))
>elfring at Sonne:~/Projekte/Bau/C++/test-statistic-server1/local> 0 connections were handled.
>But I wonder about the following error message then.
>elfring at Sonne:~/Projekte/Python> /usr/bin/python3 
>~/Projekte/Python/ --server_id localhost 
>--server_port 35529
>Using Python version:
>3.7.2 …
>Traceback …:
>>  File "/home/elfring/Projekte/Python/", line 17, in send_data
>    so.connect((args.server_id, args.server_port))
>ConnectionRefusedError: [Errno 111] Connection refused
>How should this inter-process communication difficulty be resolved?

It looks like the service isn't listening at the time the so.connect is 
called. Are you doing it before the service is ready?

Otherwise you need to print out the server_id and port, and examine the 
system to see if that address/port is in LISTEN state. Running "netstat 
-an" on the system running the service is a useful way to do this.

Hmm, look like your "ss" command effectively does that.

I'd fall back to the connect then: check that it really is using the 
correct address/port. Print them out.

Also, it can be very useful to strace the client process, eg:

  strace -e trace=network /usr/bin/python3 ~/Projekte/Python/ --server_id localhost --server_port 35529

You can also strace the running service process:

  strace -e trace=network -p pid-of-service-process-here

to see if it is responding in any way to the client connect.

Also, on the service side it isn't enough to create the service socket, 
you also need to do an accept IIRC. If you're using Python's socket 
library the service classes do that for you.

Cameron Simpson <cs at>

More information about the Python-list mailing list