简体   繁体   English

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

[英]Python ConnectionRefusedError: [Errno 61] Connection refused

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.我和我的朋友正在制作一个聊天程序,但我们不断收到错误 ConnectionRefusedError: [Errno 61] Connection refused We are on different.networks by the way。 Here is my code for the server这是我的服务器代码

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))

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.这可能无法回答您原来的问题,但我遇到了这个错误,这只是因为我没有先启动服务器进程来监听我选择测试的端口上的 localhost (127.0.0.1)。 In order for the client to connect to localhost, a server must be listening on localhost.为了让客户端连接到本地主机,服务器必须监听本地主机。

'127.0.0.1' means local computer - so client connents with server on the same computer. '127.0.0.1'表示本地计算机 - 因此客户端与同一台计算机上的服务器一致。 Client have to use IP from server - like 192.168.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/ifconfig returns local IP (like 192.168.0.1 ) which is visible only in local network. ipconfig/ifconfig返回仅在本地网络中可见的本地 IP(如192.168.0.1 )。 Then you may need external IP and setting (redirections) on your and provider routers.然后,您可能需要在您的路由器和提供商路由器上设置外部 IP 和设置(重定向)。 External IP can be IP of your router or provider router.外部IP可以是您的路由器或提供商路由器的 IP。 You can see your external IP when you visit pages like this http://httpbin.org/ip .当您访问像http://httpbin.org/ip这样的页面时,您可以看到您的外部 IP。 But it can still need some work nad it be bigger problem.但它仍然需要一些工作,否则它会成为更大的问题。

You need simply to start server at first, and then run the client_code.您只需首先启动服务器,然后运行 client_code。 In VS Code i've opened 2 terminals.在 VS Code 中,我打开了 2 个终端。 One for the server_code to be running While True , and the other one for the client_code一个用于 server_code 运行While True ,另一个用于 client_code

So this may not fix your question specifically but it fixed mine and it can help someone else I work with vscode and I use some extension that runs my code so when you want to run your server run it on your CMD or Terminal and run your client in vscode it helped me (maybe importat I work on mac so maybe spesific OS problem)所以这可能不会具体解决你的问题,但它解决了我的问题,它可以帮助我使用 vscode 的其他人,我使用一些运行我的代码的扩展,所以当你想运行你的服务器时,在你的 CMD 或终端上运行它并运行你的客户端在 vscode 中它帮助了我(也许重要的是我在 mac 上工作所以可能是特定的操作系统问题)

If you are connecting to a host:port that is open but there is no service bound to it you may see this IIRC.如果您正在连接到一个开放的主机:端口但没有绑定到它的服务,您可能会看到这个 IIRC。 Eg with ssh you sometimes see this while attempting to connect to a server that is booting but sshd is not running.例如,对于 ssh,您有时会在尝试连接到正在启动但 sshd 未运行的服务器时看到此信息。

此代码不适用于聊天,您必须使用解锁套接字选择模块或其他异步模块

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ConnectionRefusedError:[Errno 61] Mac 上的连接被拒绝 Python3.8.5 - ConnectionRefusedError: [Errno 61] Connection refused Python3.8.5 on Mac Python JavaScript爬网:ConnectionRefusedError:[Errno 61]连接被拒绝 - Python JavaScript Scraping: ConnectionRefusedError: [Errno 61] Connection refused ConnectionRefusedError:[Errno 111]连接被拒绝 - ConnectionRefusedError: [Errno 111] Connection refused ConnectionRefusedError:[Errno 61]连接被拒绝&无法连接到端点URL:http://127.0.0.1:3001 - ConnectionRefusedError: [Errno 61] Connection refused & Could not connect to the endpoint URL: http://127.0.0.1:3001 Ftplib ConnectionRefusedError: [Errno 111] 连接被拒绝 (python 3.5) - Ftplib ConnectionRefusedError: [Errno 111] Connection refused (python 3.5) python 套接字返回 ConnectionRefusedError: [Errno 111] 连接被拒绝 - python socket return ConnectionRefusedError: [Errno 111] Connection refused python-valve rcon minecraft'ConnectionRefusedError:[Errno 111]连接被拒绝' - python-valve rcon minecraft 'ConnectionRefusedError: [Errno 111] Connection refused' Python ConnectionRefusedError:[Errno 111] Docker 容器上的连接被拒绝 - Python ConnectionRefusedError: [Errno 111] Connection refused on Docker container 使用 python ftplib 下载但得到 ConnectionRefusedError: [Errno 111] Connection refused - use python ftplib downloading but get ConnectionRefusedError: [Errno 111] Connection refused Sendmail 错误[61] 连接被拒绝 - Sendmail Errno[61] Connection Refused
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM