pythonpython-3.xsockets

ConnectionRefusedError: [Errno 61] Connection refused on Mac (Python 3.8.5)


I'm trying to connect to myself with 2 little Python socket programs.

1st program:

#server.py

import socket

server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = '127.0.0.1' #L'IP du Serveur
port = 1234 #data transfering port

server.bind((host,port)) #bind server
server.listen(5)

client, addr = server.accept()
print("Got Connection from",addr)

client.send("Hello World :)".encode('UTF-8')) #send data to client
msg = client.recv(1024)
print(msg.decode('UTF-8'))
input()

2nd program:

#client.py

import socket

server = socket.socket()
host = '127.0.0.1' #L'IP du Serveur
port = 1234
server.connect((host,port))

msg =server.recv(1024)
print(msg.decode('UTF-8'))

server.send('Client Online ...'.encode('UTF-8'))
input()

I first run server.py, no problems. Then, I run client.py but when I run it I have:

    Traceback (most recent call last):
      File "/Users/user/Documents/client.py", line 8, in <module>
        server.connect((host,port))
    ConnectionRefusedError: [Errno 61] Connection refused
    >>> 

I tried multiple things like deactivating my firewall, setting my IP to 192.168.1.x, but still have the same error message. I also sent that to one of my friends that is on a PC (I'm on a Mac) and he had no problems. So I guess that the problem is because of the fact that I have a Mac. Someone have an answer or an explanation?


Solution

  • I was coding with IDLE. It was the problem. I guess that IDLE has a protection that doesn't allow people to do sockets. So I just went to Terminal and it finally works.