ConnectionRefusedError: [Errno 111] Connection Refused error in Python

This error indicates that the client cannot connect to the port on the server scripting system. Since I can ping the server, it probably won't work.

This can be caused by a number of reasons, such as incorrect routing to the destination. The second possibility is that you have a firewall between your client and server, it may be on the server or on the client.

There shouldn't be any routers or firewalls that could stop communication because based on your network address, both the server and client should be on the same LAN.


Why ConnectionRefusedError: [Errno 111] Connection refused occurs in Python

This error occurs when the client cannot reach the server due to an invalid IP or port, or the address is not unique and is already in use by another server.

Connection refused errors also occur when the server is not running, so the client cannot access the server as the server should accept the connection first.

Code example:

# server code
import socket

s = socket.socket()
host = socket.gethostname()
port = 1717
s.bind((host, port))

s.listen(5)
while True:
    c,addr = s.accept()
    print("Got connection ", addr)
    c.send("Meeting is at 10am")
    c.close()
# client code
import socket

s = socket.socket()
host = '192.168.1.2'
port = 1717

s.connect((host,port))
print(s.recv(1024))
s.close

Output:

socket.error: [Errno 111] Connection refused

How to solve ConnectionRefusedError: [Errno 111] Connection refused error in Python

Try to make the receiving socket as accessible as possible. Maybe the accessibility will only happen on one interface and this won't affect the LAN.

On the other hand, one scenario could be that it listens exclusively on address 127.0.0.1, preventing it from establishing connections with other hosts.

Code example:

import socket

s = socket.socket()
host = socket.gethostname()
port = 1717
s.bind(('', port))

s.listen(5)
while True:
    c,addr = s.accept()
    print("Got connection ", addr)
    c.send("The Meeting is at 10 am")
    c.close()
import socket

s = socket.socket()
host = socket.gethostname()
port = 1717
s.bind(('', port))

s.connect((host, port))
print(s.recv(1024))
s.close()

Output:

Got connection('192.168.1.2')
The meeting is at 10 am

When you run the command python server.py you will receive the message Got connection. Also when you run the command python client.py you will receive a message from the server.

DNS resolution may be another reason behind this issue. Since socket.gethostname() returns a hostname, if the operating system cannot convert it to a local address, an error will be returned.

Linux operating systems can edit the hosts file by adding a line.

host = socket.gethostname()
port = 1717
s.bind((host,port))
Use gethostbyname
host = socket.gethostbyname("192.168.1.2")
s.bind((host, port))

Therefore, you must use the same technology on both the client and server sides to access the host. For example, you can apply the above procedure to a customer case.

You can also access via the local host name hostnamehost = socket.gethostname() or a specific name for the local host host = socket.gethostbyname("localhost").

host = socket.gethostname()
s.connect((host, port))
host = socket.gethostbyname("localhost")
s.connect((host, port))

Summarize

ConnectionRefusedError in Python occurs when the client cannot connect to the server. Several reasons include the client not knowing the IP or port address, and the server not being running when the client wants to connect.

Several methods mentioned above can resolve this connection issue.

Guess you like

Origin blog.csdn.net/fengqianlang/article/details/134741333