Python ConnectionRefusedError: [Errno 61] 连接被拒绝

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40814712/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 00:02:14  来源:igfitidea点击:

Python ConnectionRefusedError: [Errno 61] Connection refused

pythonsocketserrno

提问by Stan Theo Hettinga

Ive seen similar questions but they I couldn't fix this error. Me and my friend are making a chat program but we keep getting the error ConnectionRefusedError: [Errno 61] Connection refused We are on different networks by the way. Here is my code for the server

我见过类似的问题,但我无法解决这个错误。我和我的朋友正在制作一个聊天程序,但我们不断收到错误 ConnectionRefusedError: [Errno 61] 连接被拒绝 顺便说一下,我们在不同的网络上。这是我的服务器代码

import socket

def socket_create():
try:

    global host
    global port
    global s
    host = ''
    port = 9999
    s = socket.socket()

except socket.error as msg:
    print("Socket creation error" + str(msg))

#Wait for client, Connect socket and port
def socket_bind():
try:
    global host
    global port
    global s
    print("Binding socket to port: " + str(port)) 
    s.bind((host, port))
    s.listen(5)
except socket.error as msg:
    print("Socket binding error" + str(msg) + "\n" + "Retrying...")
    socket_bind

#Accept connections (Establishes connection with client) socket has to       be listining
def socket_accept():
   conn, address = s.accept()
   print("Connection is established |" + " IP:" + str(address[0]) + "|    port:" + str(address[1]))
chat_send(conn)


def chat_send(conn):
 while True:
    chat =input()
    if len(str.encode(chat)) > 0:
        conn.send(str.encode(chat))
        client_response = str(conn.recv(1024), "utf-8")
    print(client_response)
def main():
socket_create()
socket_bind()
socket_accept()

main()

And my client code

还有我的客户端代码

import socket


#connects to server
s = socket.socket()
host = '127.0.0.1'
port = 9999
s.connect((host, port))

#gets chat
while True:
    data = s.recv(1024)
    print (data[:].decode("utf-8")) 
    chat = input()
    s.send(str.encode(chat))

回答by furas

'127.0.0.1'means local computer - so client connents with server on the same computer. Client have to use IPfrom server - like 192.168.0.1.

'127.0.0.1'表示本地计算机 - 因此客户端与同一台计算机上的服务器一致。客户端必须使用IP类似服务器的192.168.0.1.

Check on server:

在服务器上检查:

on Windows (in cmd.exe)

在 Windows 上(在cmd.exe

ipconfig

on Linux (in console)

在 Linux 上(在控制台中)

ifconfig

But if you are in different networks then it may not work. ipconfig/ifconfigreturns local IP (like 192.168.0.1) which is visible only in local network. Then you may need external IP and setting (redirections) on your and provider routers. External IPcan be IP of your router or provider router. You can see your external IP when you visit pages like this http://httpbin.org/ip. But it can still need some work nad it be bigger problem.

但是如果你在不同的网络中,那么它可能不起作用。ipconfig/ifconfig返回192.168.0.1仅在本地网络中可见的本地 IP(如)。然后,您可能需要在您的路由器和提供商路由器上设置外部 IP 和设置(重定向)。外部IP可以是您的路由器或提供商路由器的 IP。当您访问像http://httpbin.org/ip这样的页面时,您可以看到您的外部 IP 。但它仍然需要一些工作,否则它会成为更大的问题。

回答by Dan Kowalczyk

This may not answer your original question, but I encountered this error and it was simply that I had not starting the server process first to listen to localhost (127.0.0.1) on the port I chose to test on. In order for the client to connect to localhost, a server must be listening on localhost.

这可能无法回答您原来的问题,但我遇到了这个错误,这只是因为我没有先启动服务器进程来监听我选择测试的端口上的 localhost (127.0.0.1)。为了让客户端连接到本地主机,服务器必须监听本地主机。