Python套接字编程-ConnectionRefusedError:[WinError 10061]无法建立连接,因为目标计算机主动拒绝了它 [英] Python Socket Programming - ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it

查看:355
本文介绍了Python套接字编程-ConnectionRefusedError:[WinError 10061]无法建立连接,因为目标计算机主动拒绝了它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用客户端和服务器进行有关python中套接字编程的任务.我目前在Windows 10上.在开始有关分配的小细节之前,我一直在尝试简单地连接服务器和客户端.

I'm doing an assignment regarding socket programming in python using a client and server. 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.

每次尝试运行客户端文件时,都会出现此错误

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

我已经打开了防火墙端口,但还是一无所获.我尝试在客户端和服务器文件中都用'',0.0.0.0,socket.gethostname()替换主机,但错误仍然存​​在.我什至尝试了不同的端口号,但没有区别.我尝试在Ubuntu和Max上运行此代码,但遇到相同的错误-连接被拒绝.我一直在研究许多解决方案,但仍然没有找到可行的解决方案.任何帮助将不胜感激!

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. 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. I've been researching for many solutions but I still have yet to find one that works. Any help would be greatly appreciated!

注意:此代码已在线获取,但它实际上是我需要完成的基础.tcpclient.py

Note: this code was taken online but it's essentially the basis of what I need to accomplish. 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()

推荐答案

我在服务器中成功!

我的服务器python脚本如下:

My server python script is below:

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

我的客户端python脚本如下:

My Client python script is below:

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.服务器的主机必须为

1.The host of the Server must is

'0.0.0.0'

'0.0.0.0'

以便python脚本可以使用服务器中的所有接口

So that the python script could user all interfaces in the server

2.我已经通过提示符找到问题的错误:

2.I have find the question's error through the prompt:

TypeError:需要一个类似字节的对象,而不是'str'

TypeError: a bytes-like object is required, not 'str'

这意味着'send'方法中的每个字符串消息都需要转换为'bytes-like object',所以正确的是

It means every string message in the 'send' method need to convert to 'bytes-like object',So the correct is

s.send(b"hello")

s.send(b"hello")

重要的是,这是 b'hello',而不是'hello'

It is important that this is b'hello' not is 'hello'

这篇关于Python套接字编程-ConnectionRefusedError:[WinError 10061]无法建立连接,因为目标计算机主动拒绝了它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆