简体   繁体   English

WinError 10061,Python 服务器和客户端连接问题

[英]WinError 10061, Python Server and Client connection problem

I have two scripts: server.py and client.py and I am trying to connect them globally.我有两个脚本: server.pyclient.py ,我正在尝试全局连接它们。 They are something like this:它们是这样的:

server.py : server.py

import socket

server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(("", 5050))

while True:
    conn, addr = server.accept()

client.py : client.py

import socket
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect(("[my public ip (don\'t want to give away)]", 5050))

When I run server.py , everything is fine.当我运行server.py时,一切都很好。 When I try to connect with client.py , I get the error message: ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it .当我尝试与client.py连接时,我收到错误消息: ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it I have Firewall turned off.我关闭了防火墙。

Also, I have tried these questions but did not solve the problem:另外,我尝试了这些问题,但没有解决问题:
No connection could be made because the target machine actively refused it? 因为目标机器主动拒绝而无法建立连接?
How to fix No connection could be made because the target machine actively refused it 127.0.0.1:64527 解决方法 由于目标机器主动拒绝,无法建立连接 127.0.0.1:64527
No connection could be made because the target machine actively refused it 127.0.0.1:3446 无法建立连接,因为目标机器主动拒绝它 127.0.0.1:3446
No connection could be made because the target machine actively refused it 127.0.0.1 无法建立连接,因为目标机器主动拒绝它 127.0.0.1
Errno 10061 : No connection could be made because the target machine actively refused it ( client - server ) Errno 10061:无法建立连接,因为目标机器主动拒绝它(客户端-服务器)

In your bind instead of "" you should be writing "0.0.0.0" to get all connections.在您的绑定而不是“”中,您应该编写“0.0.0.0”以获取所有连接。 Also, if you run the server and the client on the same computer you should use "127.0.0.1" instead of your public IP address since the connection is being done in your own machine.此外,如果您在同一台计算机上运行服务器和客户端,您应该使用“127.0.0.1”而不是您的公共 IP 地址,因为连接是在您自己的计算机上完成的。 If you run each in different computers you can use the computer's local IP address, again, no need for public IP address (you can find it with cmd- write IPCONFIG and it will be shown, most of the times it looks liks "192.168.xx". I'll add codes that worked for me here for you to use:如果你在不同的计算机上运行每一个,你可以使用计算机的本地 IP 地址,同样,不需要公共 IP 地址(你可以用 cmd-write IPCONFIG 找到它,它会显示出来,大多数时候它看起来像“192.168. xx"。我将在此处添加对我有用的代码供您使用:

server.py服务器.py

import socket
server_socket = socket.socket()
server_socket.bind(('0.0.0.0', 5050))
server_socket.listen(1)
(client_socket, client_address) = server_socket.accept()
client_name = client_socket.recv(1024)
what_to_send = 'Hello ' + client_name.decode()
client_socket.send(what_to_send.encode())
client_socket.close()
server_socket.close()

client.py客户端.py

import socket
my_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
my_socket.connect(('127.0.0.1', 5050))
name = input("Enter your name: ")
my_socket.send(name.encode())
data = my_socket.recv(1024)
print(data.decode())
my_socket.close()

Did you port forward your local IP and TCP port on your router?您是否在路由器上转发了本地 IP 和 TCP 端口?

If you didn't, you will need to log in to your router through 192.168.0.1 (Depends on the setting on your router) and got to port forwarding and add a rule (for my router) stating your local IP for your server and TCP port 5050 (from your code) and save it.如果你没有,你需要通过192.168.0.1登录到你的路由器(取决于你的路由器上的设置)并进行端口转发并添加一条规则(对于我的路由器),说明你的服务器的本地 IP 和TCP 端口5050 (来自您的代码)并保存。

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

相关问题 Python socket问题-ConnectionRefusedError: [WinError 10061] - Python socket problem-ConnectionRefusedError: [WinError 10061] Python ConnectionRefusedError:[WinError 10061] - Python ConnectionRefusedError: [WinError 10061] Python中Selenium Webdriver的安全/连接问题[WinError 10061] - Security/Connection Issue with Selenium Webdriver in Python [WinError 10061] Python,tcpServer tcpClient,[WinError 10061] - Python, tcpServer tcpClient, [WinError 10061] selenium ConnectionRefusedError:python 3 中的 [WinError 10061] - selenium ConnectionRefusedError: [WinError 10061] in python 3 WinError 10061-目标计算机拒绝连接? - WinError 10061 - Target machine refused connection? 无法连接到本地 pyftpdlib FTP 服务器:[WinError 10061] 无法建立连接 - Cant connect to local pyftpdlib FTP server: [WinError 10061] No connection could be made Python 请求中的大量“ConnectionRefusedError:[WinError 10061]” - Numerous "ConnectionRefusedError: [WinError 10061]" in Python requests Python Socket 编程 - ConnectionRefusedError: [WinError 10061] 无法建立连接,因为目标机器主动拒绝了它 - Python Socket Programming - ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it Python套接字编程-ConnectionRefusedError:[WinError 10061] - Python Socket Programming - ConnectionRefusedError: [WinError 10061]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM