Python 中 ConnectionRefusedError: [Errno 61] Connection Refused 错误
有时在设计客户端-服务器程序时,您可能会遇到错误 ConnectionRefusedError。 当您的客户端程序因某些编码问题无法连接到服务器程序时,就会发生这种情况。
本文将展示我们如何在 Python 中获取 ConnectionRefusedError。 此外,我们将通过使用必要的示例和解释来讨论该主题,以使该主题更容易理解。
Python中 ConnectionRefusedError 错误是如何产生的
正如我们已经讨论过的,这个错误主要发生在客户端程序无法连接到服务器时。 为了理解这些,让我们考虑下面共享的客户端-服务器示例程序。
让我们看一下下面的服务器程序示例代码。
import socket
def ServerProgram():
host = socket.gethostname()
port = 5000
ServerSocket = socket.socket()
ServerSocket.bind((host, port))
ServerSocket.listen(2)
conn, ClientAddress = ServerSocket.accept()
print("Connection from: " + str(ClientAddress))
while True:
ClientMsg = conn.recv(1024).decode()
if not ClientMsg:
break
print("from connected user: " + str(ClientMsg))
ClientMsg = input(' -> ')
conn.send(ClientMsg.encode())
conn.close()
if __name__ == '__main__':
ServerProgram()
请注意
,我们在上述程序中将端口设置为 5000。 现在看看我们的客户端程序。
import socket
def ClientProgram():
host = socket.gethostname()
port = 5001
ClientSocket = socket.socket()
ClientSocket.connect((host, port))
ClientMessage = input(" -> ")
while ClientMessage.lower().strip() != 'bye':
ClientSocket.send(ClientMessage.encode())
ServerMsg = ClientSocket.recv(1024).decode()
print('Received from server: ' + ServerMsg)
ClientMessage = input(" -> ")
ClientSocket.close()
if __name__ == '__main__':
ClientProgram()
我们故意在我们的客户端程序上犯了一个错误:我们将客户端程序上的端口设置为 5001。现在当您在服务器程序之后运行客户端程序时,您将收到如下所示的错误消息。
Traceback (most recent call last):
File "F:\Python\client.py", line 25, in <module>
ClientProgram()
File "F:\Python\client.py", line 9, in ClientProgram
ClientSocket.connect((host, port)) # connect to the server
ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it
发生此错误是因为客户端程序无法连接到服务器。 如果您先启动服务器程序,也会发生此错误。
在这种情况下,客户端程序将找不到要连接的服务器程序。
如何修复 Python 中的 ConnectionRefusedError
我们可以使用确切的服务器端口 5000 轻松修复上述错误。现在更新代码后,它将如下所示。
import socket
def ClientProgram():
host = socket.gethostname()
port = 5000 ## We fixed here.
ClientSocket = socket.socket()
ClientSocket.connect((host, port))
ClientMessage = input(" -> ")
while ClientMessage.lower().strip() != 'bye':
ClientSocket.send(ClientMessage.encode())
ServerMsg = ClientSocket.recv(1024).decode()
print('Received from server: ' + ServerMsg)
ClientMessage = input(" -> ")
ClientSocket.close()
if __name__ == '__main__':
ClientProgram()
执行客户端程序后,您将在客户端获得以下输出。
-> Hi Server
Received from server: Hi Client
-> This is a message from the client
Received from server: This is a message from the server
您将在服务器端看到以下输出。
Connection from: ('192.168.0.159', 11418)
from connected user: Hi Server
-> Hi Client
from connected user: This is a message from the client
-> This is a message from the server
请注意,在运行客户端程序之前必须先运行服务器程序; 否则,你会得到同样的错误。
请注意,此处讨论的命令和程序是用 Python 编程语言编写的。
相关文章
Pandas DataFrame DataFrame.shift() 函数
发布时间:2024/04/24 浏览次数:133 分类:Python
-
DataFrame.shift() 函数是将 DataFrame 的索引按指定的周期数进行移位。
Python pandas.pivot_table() 函数
发布时间:2024/04/24 浏览次数:82 分类:Python
-
Python Pandas pivot_table()函数通过对数据进行汇总,避免了数据的重复。
Pandas read_csv()函数
发布时间:2024/04/24 浏览次数:254 分类:Python
-
Pandas read_csv()函数将指定的逗号分隔值(csv)文件读取到 DataFrame 中。
Pandas 多列合并
发布时间:2024/04/24 浏览次数:628 分类:Python
-
本教程介绍了如何在 Pandas 中使用 DataFrame.merge()方法合并两个 DataFrames。
Pandas loc vs iloc
发布时间:2024/04/24 浏览次数:837 分类:Python
-
本教程介绍了如何使用 Python 中的 loc 和 iloc 从 Pandas DataFrame 中过滤数据。
在 Python 中将 Pandas 系列的日期时间转换为字符串
发布时间:2024/04/24 浏览次数:894 分类:Python
-
了解如何在 Python 中将 Pandas 系列日期时间转换为字符串