简体   繁体   English

Python Socket 编程 - ConnectionRefusedError: [WinError 10061] 无法建立连接,因为目标机器主动拒绝了它

[英]Python Socket Programming - ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it

I'm doing an assignment regarding socket programming in python using a client and server.我正在使用客户端和服务器在 python 中进行有关套接字编程的作业。 I'm currently on windows 10. Before getting into the little details of the assignment, I've been trying to simply connect the server and client.我目前使用的是 Windows 10。在进入任务的小细节之前,我一直在尝试简单地连接服务器和客户端。

Every time I try to run the client file, I would get this error每次我尝试运行客户端文件时,都会收到此错误

File "tcpclient.py", line 9, in <module>
    s.connect((host, port))
ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it

I have opened the firewall ports and still nothing.我打开了防火墙端口,仍然没有。 I've tried replacing host with '', 0.0.0.0, socket.gethostname() in both the client and server file but the error still persists.我尝试在客户端和服务器文件中用 '', 0.0.0.0, socket.gethostname() 替换主机,但错误仍然存​​在。 I've even tried different port numbers but it made no difference.我什至尝试过不同的端口号,但没有任何区别。 I've tried running this code on Ubuntu and Max and I get the same error - connection refused.我试过在 Ubuntu 和 Max 上运行这段代码,我得到了同样的错误——连接被拒绝。 I've been researching for many solutions but I still have yet to find one that works.我一直在研究许多解决方案,但我仍然没有找到一个有效的解决方案。 Any help would be greatly appreciated!任何帮助将不胜感激!

Note: this code was taken online but it's essentially the basis of what I need to accomplish.注意:此代码是在线获取的,但它本质上是我需要完成的工作的基础。 tcpclient.py tcpclient.py

import socket

host = '127.0.0.1'
port = 80
buffer_size = 1024
text = "Hello, World!"

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, port))
s.send(text)
data = s.recv(buffer_size)
s.close()

print("received data:", data)

tcpserver.py tcpserver.py

import socket

host = '127.0.0.1'
port = 80
buffer_size = 20  

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((host, port))
s.listen(1)

conn, addr = s.accept()
print 'Connection address:', addr
while 1:
    data = conn.recv(buffer_size)
if not data: break
print("received data:", data)
conn.send(data)  # echo
conn.close()

For client.py对于客户端.py

import socket
host = '127.0.0.1'
port = 9879
buffer_size = 1024
text = "Hello, World!"
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, port))
text = text.encode('utf-8')
s.send(text)
data = s.recv(buffer_size)
s.close()
print("received data:", data)

For server.py对于 server.py

import socket
mysocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
buffer_size = 1024
text = "Hello, World!"
mysocket.bind(('127.0.0.1', 9879))
mysocket.listen(5)
(client, (ip,port)) = mysocket.accept()
print(client, port)
client.send(b"knock knock knock, I'm the server")
data = client.recv(buffer_size)
print(data.decode())
mysocket.close()

Just change the port number and it will work and if you are in python3 then you will have to encode and decode as socket recieves and sends only binary strings.只需更改端口号,它就可以工作,如果你在 python3 中,那么你将不得不在套接字接收和发送二进制字符串时进行编码和解码。

Just use a different port.只需使用不同的端口。 Both the client and server should have the same port and host if not it won't work.客户端和服务器都应该具有相同的端口和主机,否则它将无法工作。 Make sure to run the server before the client script.确保在客户端脚本之前运行服务器。

I have succeed in my server!我的服务器成功了!

My server python script is below:我的服务器 python 脚本如下:

    import socket
    host='0.0.0.0'
    port=2345
    s=socket.socket()
    s.bind((host,port))
    s.listen(2)
    while True:
    conn,addr=s.accept()
    print("Connected by",addr)
    data=conn.recv(1024)
    print("received data:",data)
    conn.send(data)
    conn.close()

My Client python script is below:我的客户端 python 脚本如下:

import socket
s=socket.socket()
host="xx.xx.xx.xx"       #This is your Server IP!
port=2345
s.connect((host,port))
s.send(b"hello")
rece=s.recv(1024)
print("Received",rece)
s.close()

There is two points needed to be careful in the script:脚本中有两点需要注意:

1.The host of the Server must is 1.Server的主机必须是

'0.0.0.0' '0.0.0.0'

So that the python script could user all interfaces in the server这样python脚本就可以使用服务器中的所有接口

2.I have find the question's error through the prompt: 2.通过提示找到问题的错误:

TypeError: a bytes-like object is required, not 'str' TypeError:需要一个类似字节的对象,而不是“str”

It means every string message in the 'send' method need to convert to 'bytes-like object',So the correct is这意味着'send'方法中的每个字符串消息都需要转换为'bytes-like object',所以正确的是

s.send(b"hello") s.send(b"你好")

It is important that this is b'hello' not is 'hello'重要的是这是b'hello'而不是'hello'

I was following a tutorial that used threading to start the server.我正在关注使用线程启动服务器的教程。 Once I removed the threading then I was able to connect to it.一旦我删除了线程,我就可以连接到它。

暂无
暂无

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

相关问题 ConnectionRefusedError: [WinError 10061][WinError 10061] 由于目标机器主动拒绝,无法建立连接 - ConnectionRefusedError: [WinError 10061][WinError 10061] No connection could be made because the target machine actively refused it ConnectionRefusedError:[WinError 10061]无法建立连接,因为目标计算机主动拒绝了它 - ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it ConnectionRefusedError at /password-reset/ [WinError 10061] 无法建立连接,因为目标机器主动拒绝它 - ConnectionRefusedError at /password-reset/ [WinError 10061] No connection could be made because the target machine actively refused it (tkinter) ConnectionRefusedError: [WinError 10061] 由于目标机器主动拒绝,无法建立连接 - (tkinter) ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it WinError 10061 由于目标机器主动拒绝,无法建立连接 - WinError 10061 No connection could be made because the target machine actively refused it “[WinError 10061] 无法建立连接,因为目标机器主动拒绝它” - "[WinError 10061] No connection could be made because the target machine actively refused it" 发送套接字时出错[WinError 10061]无法建立连接,因为目标计算机主动拒绝将Socket从Python发送到Android - Error sending socket [WinError 10061]No connection could be made because the target machine actively refused it for Socket from Python to Android 无法建立新连接:[WinError 10061]无法建立连接,因为目标计算机主动拒绝了该连接 - Failed to establish a new connection: [WinError 10061] No connection could be made because the target machine actively refused it pymongo.errors.ServerSelectionTimeoutError:localhost:27017:[WinError 10061]无法建立连接,因为目标机器主动拒绝 - pymongo.errors.ServerSelectionTimeoutError:localhost:27017:[WinError 10061]No connection could be made because the target machine actively refused it DJANGO 电子邮件确认:[WinError 10061] 无法建立连接,因为目标机器主动拒绝它 - DJANGO EMAIL CONFIRMATION: [WinError 10061] No connection could be made because the target machine actively refused it
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM