简体   繁体   English

如何使用 Python 中的 sockets 连接全球网络上的两台随机计算机

[英]How to connect two random computers on global network using sockets in Python

I have some problems with global connection between two computers using sockets in python.在 python 中使用 sockets 的两台计算机之间的全局连接存在一些问题。 I successfully connected from my laptop, to my computer which was working on the same network.我成功地从笔记本电脑连接到在同一网络上工作的计算机。 But now I want to connect with my friend.(I have simple chat app working in console)但现在我想和我的朋友联系。(我在控制台中有一个简单的聊天应用程序)

Here is a server initialization code:这是一个服务器初始化代码:

import socket
import threading

# print(socket.gethostname())

allmsgs = []

FORMAT = "utf-8"
HEADER = 64
PORT = 5050
SERVER = socket.gethostbyname(socket.gethostname())
ADDR = (SERVER,PORT)
DISCONNECTMESSAGE = "!DISCONNECT"

connections = []
clients = []

server = socket.socket(socket.AF_INET,socket.SOCK_STREAM)

server.bind((ADDR))
#there are more code in this file, but I show only init 

And here is a client code:这是一个客户端代码:

import socket
from time import sleep
import threading

SERVER = "(my public IP which popped up when i googled it)"
FORMAT = "utf-8"
HEADER = 64
PORT = 5050
DISCONNECTMESSAGE = "!DISCONNECT"
ADDR = (SERVER,PORT)


client = socket.socket(socket.AF_INET,socket.SOCK_STREAM)

nickname = input("Enter your nickname: ")

try:
    if nickname.split()[1] == "devmode":
        devmode = True
        nickname = nickname.split()[0]
except:devmode = False
while True:
    try:
        client.connect(ADDR)
        print("Succesfully connected")
        break
    except:
        print("Unable to connect.")
        print("Next try in 1 sec")
        sleep(1)

I have the我有

ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it" error on the client's side.

I am not sure that I make all right for successful connection.我不确定我是否可以成功连接。 I know that there are a lot of materials about this theme, including stack itself, but none which I tried didn't help me.我知道有很多关于这个主题的材料,包括堆栈本身,但我尝试过的没有一个对我没有帮助。

I think your client-side server IP should be the same as server-side server IP coz in server-side code, you have assigned the local IP address whereas in client-side you have assigned your public IP. I think your client-side server IP should be the same as server-side server IP coz in server-side code, you have assigned the local IP address whereas in client-side you have assigned your public IP.

I'm assuming these two computers are not on the same network and are communicating over the internet.我假设这两台计算机不在同一个网络上,而是通过互联网进行通信。

If this is the case then you will need to set up port forwarding on your router.如果是这种情况,那么您需要在路由器上设置端口转发。 This varies by device so cannot provide any specific instructions here, but essentially when your router recevies a request from outside your home network you need to forward that request onto the computer where you are running your server application.这因设备而异,因此无法在此处提供任何具体说明,但本质上,当您的路由器从家庭网络外部接收请求时,您需要将该请求转发到运行服务器应用程序的计算机上。 You can usually access your router settings through 192.168.1.0 or 192.168.0.254 but again this varies between device.您通常可以通过 192.168.1.0 或 192.168.0.254 访问您的路由器设置,但这又因设备而异。 Under the port forwarding section you will need to enter the local ip address of the computer running your server application.在端口转发部分下,您需要输入运行服务器应用程序的计算机的本地 ip 地址。

WARNING : from your code snippet it doesn't look like you are providing any identification, authentication or encryption from any clients that want to connect to you.警告:从您的代码片段来看,您似乎没有从任何想要连接到您的客户端提供任何标识、身份验证或加密。 You absolutely want to implement a number of features to restrict who and what can access the service on your computer before setting up any port forwarding, and this is not trivial, but maybe be a good and fun the learning experience.在设置任何端口转发之前,您绝对希望实现一些功能来限制谁和什么可以访问您的计算机上的服务,这不是微不足道的,但可能是一个很好和有趣的学习体验。 There are many bad robots crawling the web looking for open services to attack and the above code would provide an easy target to exploit.有许多不良机器人在爬取 web 寻找开放服务进行攻击,上面的代码将提供一个容易被利用的目标。

Alternatively I would look a setting up some kind of VPN service that provides the identification, authentication and encryption mechanisms for you.或者,我会寻找一种为您提供识别、身份验证和加密机制的 VPN 服务。 OpenVPN is a fairly common product for this use case. OpenVPN 是这个用例的一个相当常见的产品。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM