Python套接字通过全球公共IP地址进行连接 [英] Python Socket to connect over Global Public IP Address

查看:97
本文介绍了Python套接字通过全球公共IP地址进行连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在研究一个项目,该项目通过Python将位于不同位置的计算机连接在一起.最初,在测试时,我使用我的私有IP地址(当时我还不知道它是私有的)来连接与我的同一个网络上的计算机.但是,当我尝试使用位于不同位置的不同网络上的计算机执行此操作时,它就根本不起作用.

I have been working on a project to connect computers located in different locations together through Python. Initially, while testing, I used my private IP address (I did not know it was private at the time) to connect computers on the same network as mine. But as soon as I tried doing this with computers located on different networks in different locations, it simply did not work.

我认为这是因为程序正在使用我的计算机的本地IP地址,该地址只能连接到同一网络上的计算机.这是我的简化程序:

And I assume this is because the program is using the local IP address of my computer that can connect only to computers on the same network. Here are my simplified programs:

这是我的服务器端脚本:

Here is my server-side script:

server = socket.gethostbyname(socket.gethostname()) # 10.128.X.XXX which is the Internal IP
print(server)
port = 5555
clients = 0

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((server, port))

s.listen(2)
print("Waiting for connection...")

while True:
    conn, addr = s.accept()
    print("Connected to: ", addr)

    conn.send(str.encode(f"{clients}"))
    clients += 1

这是我的客户端脚本:

class Network:
    def __init__(self):
        self.client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.server = "10.128.0.2"
        self.port = 5555
        self.addr = (self.server, self.port)
        self.id = int(self.connect())

    def connect(self):
        self.client.connect(self.addr)
        return self.client.recv(2048).decode()

network = Network()
print(f"Connected as client {network.id}")

现在,当我尝试用全局IP地址替换私有IP地址(如此处指定:

Now when I tried replacing the private IP address with the global IP address (as specified here: How do I get the external IP of a socket in Python?) I got the following error:

# Getting the Global IP Address

from requests import get
server = get("https://api.ipify.org").text

s.bind((server, port))
OSError: [WinError 10049] The requested address is not valid in its context

我尝试了很多有关如何使用不同网络在不同位置的多台计算机之间进行通信(以字符串形式传输少量数据)的尝试,但是我还没有真正找到解决方案.有办法可以做到吗?

I have tried searching a lot on how to communicate (transfer small amounts of data as strings) between multiple computers located in different locations using different networks, but I haven't really gotten a solution. Is there a way that I can do this?

推荐答案

在服务器中,您始终使用local IP(计算机中网卡之一的IP或使用所有网卡的0.0.0.0)

In server you always use local IP (it is IP of one of network cards in computer or 0.0.0.0 to use all network cards)

s.bind( (local_IP, port) )

# or 

s.bind( ('0.0.0.0', port) ) 

在客户端中,您使用external IP

s.connect( (external_IP, port) )


外部客户端使用external IP与您的Internet提供商路由连接,并且该路由器知道此external IP已分配给您的计算机,并将其重定向到您的服务器.


External client uses external IP to connect with your Internet Provider route and this router knows that this external IP is assigned to your computer and it redirects it your server.

与此同时,本地客户端可以使用local IP与同一服务器连接.

At the same time local client can use local IP to connect with the same server.

external_client --> router(externa_IP) --> server(local_IP) <-- local_client

这篇关于Python套接字通过全球公共IP地址进行连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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