Solved for telnet by Rene Sockets in Python code. Connection refused?

Questions about Wi-Fi and other network devices, file sharing, firewalls, connection sharing etc
Forum rules
Before you post read how to get help. Topics in this forum are automatically closed 6 months after creation.
Locked
sandbagger
Level 3
Level 3
Posts: 197
Joined: Mon Dec 28, 2020 12:36 pm

Solved for telnet by Rene Sockets in Python code. Connection refused?

Post by sandbagger »

Hi all

got a simple python prog to just test sending stuff and I am getting "ConnectionRefusedError: [Errno 111] Connection refused" . I am opening port 1234 on the "server" side and that seems to be working fine

Code: Select all

dave@deepthought:~$ netstat -an |grep 1234
tcp        0      0 127.0.1.1:1234          0.0.0.0:*               LISTEN  
when I try to connect to 1234 using the "client" code I get the "ConnectionRefusedError: [Errno 111] Connection refused" error.

so I thought try telnet

Code: Select all

ave@deepthought:~$ telnet 127.0.0.1 1234
Trying 127.0.0.1...
telnet: Unable to connect to remote host: Connection refused

maybe the ufw is running NOPE.

Code: Select all

 sudo ufw status
Status: inactive
how about, just in case, updating the NON runnning ufw

Code: Select all

sudo ufw allow 1234/tcp
Rules updated
Rules updated (v6)

NOPE still nothing?????

Code: Select all

telnet 127.0.0.1 1234
Trying 127.0.0.1...
telnet: Unable to connect to remote host: Connection refused
Can someone help me out??? the code is fine I have used it before on ubuntu. I'm happy to post the code but didn't think it was relevant.

thanks
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 2 times in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
rene
Level 20
Level 20
Posts: 12240
Joined: Sun Mar 27, 2016 6:58 pm

Re: Sockets in Python code. Connection refused????

Post by rene »

Server's listening on 127.0.1.1, not 127.0.0.1 (former is the address where historically local dnsmasq listened on, if that helps, i.e, if it's not just a typo but something you did in /etc/hosts or alike).
sandbagger
Level 3
Level 3
Posts: 197
Joined: Mon Dec 28, 2020 12:36 pm

Re: Sockets in Python code. Connection refused????

Post by sandbagger »

GREAT spot but didn't work when I changed it

Code: Select all

telnet 127.0.1.1 1234
Trying 127.0.1.1...
telnet: Unable to connect to remote host: Connection refused
dave@deepthought:~$ 

Code: Select all

dave@deepthought:~$ netstat -an |grep 1234
tcp        0      0 127.0.1.1:1234          0.0.0.0:*               LISTEN 
Thanks for the GREAT spot
sandbagger
Level 3
Level 3
Posts: 197
Joined: Mon Dec 28, 2020 12:36 pm

Re: Sockets in Python code. Connection refused????

Post by sandbagger »

that was weird it WORKED when I tried it again

Code: Select all

dave@deepthought:~$ netstat -an |grep 1234
tcp        0      0 127.0.1.1:1234          0.0.0.0:*               LISTEN     
dave@deepthought:~$ telnet 127.0.1.1 1234
Trying 127.0.1.1...
Connected to 127.0.1.1.
Escape character is '^]'.

my apologies, you were right but the code STILL doesn't work. Same error message.
rene
Level 20
Level 20
Posts: 12240
Joined: Sun Mar 27, 2016 6:58 pm

Re: Sockets in Python code. Connection refused????

Post by rene »

What do you mean? Above output shows it working.

[EDIT] You likely mean the client code is still not working. You'd need to post code, but note that I myself am in bed; can't test, heck, can hardly type on this dumb tablet. Idea was that this was a quick 127.0.0.1/127.0.1.1 thread...
sandbagger
Level 3
Level 3
Posts: 197
Joined: Mon Dec 28, 2020 12:36 pm

Re: Sockets in Python code. Connection refused?

Post by sandbagger »

so I decided to check the localhost issue and I got 127.0.0.1

Code: Select all

dave@deepthought:~$ ping 0
PING 0 (127.0.0.1) 56(84) bytes of data.
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.036 ms
64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.028 ms
NOW it's listening on 127.0.0.1 and all I did was change the port number

Code: Select all

. netstat -an |grep 12345
tcp        0      0 127.0.0.1:12345         0.0.0.0:*               LISTEN  

I can telnet to it

Code: Select all

dave@deepthought:~$ telnet localhost 12345
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
Thank you for connectingConnection closed by foreign host.
But this code "should" work as it does the same thing

Code: Select all

# Import socket module
import socket			

# Create a socket object
s = socket.socket()		

# Define the port on which you want to connect
port = 12345			

# connect to the server on local computer
s.connect(('127.0.0.1', port))

# receive data from the server and decoding to get the string.
print (s.recv(1024).decode())
# close the connection
s.close()
OOPS I just read your post about the tablet SORRY!!! I'll dig into this. Have a GREAT night and a safe 2022 I marked it as SOLVED but I don't know how to show the SOLVE message on this forum. Thanks for taking the time
rene
Level 20
Level 20
Posts: 12240
Joined: Sun Mar 27, 2016 6:58 pm

Re: Solved for telnet by Rene Sockets in Python code. Connection refused?

Post by rene »

Do not immediately see what's wrong; shall try tomorrow.
sandbagger
Level 3
Level 3
Posts: 197
Joined: Mon Dec 28, 2020 12:36 pm

Re: Solved for telnet by Rene Sockets in Python code. Connection refused?

Post by sandbagger »

thank you. I can't see it. Have a great night.
rene
Level 20
Level 20
Posts: 12240
Joined: Sun Mar 27, 2016 6:58 pm

Re: Solved for telnet by Rene Sockets in Python code. Connection refused?

Post by rene »

Works fine.

Code: Select all

#!/usr/bin/env python3

import socket

s = socket.socket()
s.bind(('127.0.0.1', 1234))
s.listen()
conn, addr = s.accept()
conn.sendall(b'Thank you for connecting')
s.close()

Code: Select all

#!/usr/bin/env python3

import socket			

s = socket.socket()		
s.connect(('127.0.0.1', 1234))
print (s.recv(1024).decode())
s.close()

Code: Select all

rene@hp8k:~$ ./server.py &
[1] 3837
rene@hp8k:~$ ./client.py 
Thank you for connecting
rene@hp8k:~$
It's by the way preferred that you s.shutdown() before you s.close() but doesn't here in fact matter; unless you did something unexpected "server" sides still couldn't tell you what's wrong; works fine here.
sandbagger
Level 3
Level 3
Posts: 197
Joined: Mon Dec 28, 2020 12:36 pm

Re: Solved for telnet by Rene Sockets in Python code. Connection refused?

Post by sandbagger »

First of all thank you so much for walking into my nightmare and trying to help. I put the code into two files inside the idle ide and got the same

Code: Select all


Traceback (most recent call last):
  File "/usr/lib/python3.8/idlelib/run.py", line 559, in runcode
    exec(code, self.locals)
  File "/home/dave/j_sandbox/j_raw/1_17_22_rene_client.py", line 6, in <module>
    s.connect(('127.0.0.1', 1234))
ConnectionRefusedError: [Errno 111] Connection refused
>>> 
so this seems to be a config issue. I SWEAR on the user manual that all I've done on this machine is install LM and upgrade to LM20.3, PROMISE!!!

the connection seems to be established and the ip addressing is a tad weird. 192.168.1.219 is another machine on the lan. Nothing to do with this so I don't know how that got into the mix. The machine that both these scripts are running on is 192.168.1.42

Code: Select all

netstat -an | grep 1234
tcp        0      0 192.168.1.42:51092      192.168.1.219:1234      ESTABLISHED

I reran the client a few times to make sure that there wasn't some kind of delay. Nope

Code: Select all

========== RESTART: /home/dave/j_sandbox/j_raw/1_17_22_rene_client.py ==========
Traceback (most recent call last):
  File "/usr/lib/python3.8/idlelib/run.py", line 559, in runcode
    exec(code, self.locals)
  File "/home/dave/j_sandbox/j_raw/1_17_22_rene_client.py", line 6, in <module>
    s.connect(('127.0.0.1', 1234))
ConnectionRefusedError: [Errno 111] Connection refused
>>> 
========== RESTART: /home/dave/j_sandbox/j_raw/1_17_22_rene_client.py ==========
Traceback (most recent call last):
  File "/usr/lib/python3.8/idlelib/run.py", line 559, in runcode
    exec(code, self.locals)
  File "/home/dave/j_sandbox/j_raw/1_17_22_rene_client.py", line 6, in <module>
    s.connect(('127.0.0.1', 1234))
ConnectionRefusedError: [Errno 111] Connection refused
>>> 
========== RESTART: /home/dave/j_sandbox/j_raw/1_17_22_rene_client.py ==========
Traceback (most recent call last):
  File "/usr/lib/python3.8/idlelib/run.py", line 559, in runcode
    exec(code, self.locals)
  File "/home/dave/j_sandbox/j_raw/1_17_22_rene_client.py", line 6, in <module>
    s.connect(('127.0.0.1', 1234))
ConnectionRefusedError: [Errno 111] Connection refused
>>> 

So this is a problem MY end but I don't have the networking or linux skills to figure it out. I''ve checked the ufw, I don't have anything running except the defaults that LM cranks up. The ProtonVPN is off. Is there anything else I could have messed up?


I tried to hard code the ip address
[code

s.bind(('192.168.1.42', 1234))
and
s.connect(('192.168.1.42', 1234))

][/code]

Same deal

Code: Select all

========== RESTART: /home/dave/j_sandbox/j_raw/1_17_22_rene_client.py ==========
Traceback (most recent call last):
  File "/usr/lib/python3.8/idlelib/run.py", line 559, in runcode
    exec(code, self.locals)
  File "/home/dave/j_sandbox/j_raw/1_17_22_rene_client.py", line 6, in <module>
    s.connect(('192.168.1.42', 1234))
ConnectionRefusedError: [Errno 111] Connection refused
>>> 
AND I get the same mysterious appearance of 192.168.1.219

Code: Select all

dave@deepthought:~$ netstat -an | grep 1234
tcp        0      0 192.168.1.42:51092      192.168.1.219:1234      ESTABLISHED
dave@deepthought:~
I do have a router on the net and I have hardwired the mac addresses to the machines but I can't see how that would have any effect on this. I tried to kill the connection from the code using the shutdown and close. That didn't work. I googled how to kill the thing using netstat and gdb. No luck.

Code: Select all

1583  netstat -an | grep 1234
 1584  netstat -np
 1585  netstat -np | grep 51092
 1586  lsof -np $259253
 1587  lsof -np 259253
 1588  gdb -p gdb 259253
 1589  netstat -np | grep 51092

I'm going to reboot this machine tonight and try your code again tomorrow with a fresh "mind" thank you for your help. To be honest zmq is looking pretty good about now :-)
sandbagger
Level 3
Level 3
Posts: 197
Joined: Mon Dec 28, 2020 12:36 pm

Re: Solved for telnet by Rene Sockets in Python code. Connection refused?

Post by sandbagger »

Hi Rene
thanks for all your help. Much appreciated. This approach didn't work and I have to move on. I understand that it must be a configuration issue my end and so I'll just wait until I load another machine and try that one. Thank you again for trying. Linux is new to me and I am a simple programmer.
rene
Level 20
Level 20
Posts: 12240
Joined: Sun Mar 27, 2016 6:58 pm

Re: Solved for telnet by Rene Sockets in Python code. Connection refused?

Post by rene »

The 192.168.1.219 thing makes no sense whatsoever, and not in a "now, that's odd" sort of way but a stark raving mad one. Was as such waiting for the promised reboot but I take it then that helped none either. Noticed the "j_sandbox" thing and wondered if we're looking at some virtualized environment, but that runs afoul of an expectation that you would've mentioned and/or suspected as much yourself.

Anyways. Can then not otherwise comment. Makes no sense.
sandbagger
Level 3
Level 3
Posts: 197
Joined: Mon Dec 28, 2020 12:36 pm

Re: Solved for telnet by Rene Sockets in Python code. Connection refused?

Post by sandbagger »

Hey Rene

I just rebooted no luck same deal "connection refused" ho hum! Am I using a weird port number? This is NOT a virtual box. It's a plain vanilla load of Linux Mint 20.3, fresh reboot.

Thank you do much for even bothering to return to my nightmare.




server side

Code: Select all

#!/usr/bin/env python3
import socket
s = socket.socket()
s.bind(('192.168.1.42', 5555))
s.listen()
conn, addr = s.accept()
conn.sendall(b'Thank you for connecting')
s.close()
Client side

Code: Select all

#!/usr/bin/env python3
import socket			
s = socket.socket()		
s.connect(('192.168.1.42', 5555))
print (s.recv(1024).decode())
s.close()

Code: Select all

Python 3.8.10 (default, Nov 26 2021, 20:14:08) 
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license()" for more information.
>>> 
========== RESTART: /home/dave/j_sandbox/j_raw/1_17_22_rene_server.py ==========

========== RESTART: /home/dave/j_sandbox/j_raw/1_17_22_rene_client.py ==========
Traceback (most recent call last):
  File "/usr/lib/python3.8/idlelib/run.py", line 559, in runcode
    exec(code, self.locals)
  File "/home/dave/j_sandbox/j_raw/1_17_22_rene_client.py", line 6, in <module>
    s.connect(('192.168.1.42', 5555))
ConnectionRefusedError: [Errno 111] Connection refused
>>> 

rene
Level 20
Level 20
Posts: 12240
Joined: Sun Mar 27, 2016 6:58 pm

Re: Solved for telnet by Rene Sockets in Python code. Connection refused?

Post by rene »

Your copy-pastes do not show you starting the listener as per e.g. my above

Code: Select all

rene@hp8k:~$ ./server.py &
[1] 3837
rene@hp8k:~$ ./client.py 
Thank you for connecting
rene@hp8k:~$
Definitely seems that in your case simply nothing's listening; ./server/py would tell you if it bugged out due f.e. to a busy port.

In any case works fine for me also on a LAN IP and using port 5555. You have already denied a firewall interfering and virtualisation and I'm afraid I can't further help; still no idea what's happening there.
sandbagger
Level 3
Level 3
Posts: 197
Joined: Mon Dec 28, 2020 12:36 pm

Re: Solved for telnet by Rene Sockets in Python code. Connection refused?

Post by sandbagger »

Hi Rene
sorry I missed that :-( It works if I do it that way

Code: Select all

 chmod +x 1_17_22_rene_server.py
 chmod +x 1_17_22_rene_client.py
$ ./1_17_22_rene_server.py &
[1] 645858
$ chmod +x 1_17_22_rene_client.py
$ ./1_17_22_rene_client.py 
Thank you for connecting
$ 
thank you for staying with me on this. Have an excellent day
rene
Level 20
Level 20
Posts: 12240
Joined: Sun Mar 27, 2016 6:58 pm

Re: Solved for telnet by Rene Sockets in Python code. Connection refused?

Post by rene »

Which does beg the question as to which other way did not work? That is, yes, you're going to have to in fact be running the server to have something to connect to from the client...
sandbagger
Level 3
Level 3
Posts: 197
Joined: Mon Dec 28, 2020 12:36 pm

Re: Solved for telnet by Rene Sockets in Python code. Connection refused?

Post by sandbagger »

Hi Rene

I was running the server before the client. I ran the server code in an Idle session , then started the client in the same session and nothing happened. Thanks for your help and have an EXCELLENT day.
Locked

Return to “Networking”