Search code examples
pythonsocketsserverporthost

ConnectionRefusedError: [Errno 61] Connection refused Python3.8.5 on Mac


I'm trying to connect myself with 2 pythons little programs while using socket. 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. Than, 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 desactivate my wall fire, put my 192.168.1.x IP but still have the same message error. I also send that to one of my friends that is on a PC (I'm on a MAC) and he has 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.