简体   繁体   English

如何通过 UDP 从远程机器(客户端)向本地机器(服务器)发送 Python 的数据?

[英]How to send data with Python over UDP from remote machine (client) to local machine (server)?

I am trying to send data with Python over UDP from a remote machine in the cloud to my local machine listening.我正在尝试将 Python 的数据从云中的远程机器发送到 UDP 到我的本地机器监听。

Run from my local machine... (server)从我的本地机器运行...(服务器)

from socket import *

serverSocket = socket(AF_INET, SOCK_DGRAM)

serverSocket.bind(('0.0.0.0', 3000))

while True:
    message, address = serverSocket.recvfrom(1024)
    print(message, address)

Run from the remote machine in the cloud... (client)从云中的远程机器运行...(客户端)

from socket import *

clientSocket = socket(AF_INET, SOCK_DGRAM)

server_addr = (<WHAT GOES HERE?>, 3000)

clientSocket.sendto(b'hello world', server_addr)

I believe <WHAT GOES HERE?> should be the hostname of my local machine, but I am unfamiliar with how to expose the port on my local machine which the server is running on.我相信 <WHAT GOES HERE?> 应该是我本地机器的主机名,但我不熟悉如何在运行服务器的本地机器上公开端口。

How do I expose my local machine such that the client running on the remote cloud machine can successfully send the UDP packet to the server on the local machine?如何暴露我的本地机器,以便远程云机器上运行的客户端可以成功地将 UDP 数据包发送到本地机器上的服务器?

Thanks in advance.提前致谢。

Since the remote client is "in the cloud" (ie, is running on another machine elsewhere on the Inte.net), it will need to use the public IP that is assigned to you by your ISP.由于远程客户端“在云中”(即在 Inte.net 上其他地方的另一台机器上运行),它将需要使用您的 ISP 分配给您的公共 IP。 On the server side, you can use websites like https://api.ipify.org to discover your public IP and then give it to your client to connect to.在服务器端,您可以使用https://api.ipify.org等网站来发现您的公共 IP,然后将其提供给您的客户端进行连接。 Or, you can register a static hostname with any public domain Registrar (GoDaddy, etc) and point it to your public IP (if your IP is dynamic, there are Dynamic DNS services available to handle that scenario).或者,您可以向任何公共域注册商(GoDaddy 等)注册一个 static 主机名,并将其指向您的公共 IP(如果您的 IP 是动态的,则可以使用动态 DNS 服务来处理该情况)。

If your server machine is connected DIRECTLY to your Inte.net modem, then your public hostname/IP will route directly to your server machine.如果您的服务器机器直接连接到您的 Inte.net 调制解调器,那么您的公共主机名/IP 将直接路由到您的服务器机器。

                 -------
     ----------> | DNS | ----------
     |           -------          |
     |                           \|/
----------     ------------     --------------
| client | <-> | Internet | <-> |x:x| server |
----------     ------------     --------------

But, if your server is NOT DIRECTLY connected to your modem, but is running behind a NAT router instead, then your public hostname/IP will route to the router, not to your server machine:但是,如果您的服务器没有直接连接到您的调制解调器,而是在 NAT 路由器后面运行,那么您的公共主机名/IP 将路由到路由器,而不是您的服务器机器:

                 -------
     ----------> | DNS | ----------
     |           -------          |
     |                           \|/
----------     ------------     --------------     --------------
| client | <-> | Internet | <-> |x:x| router | <-> |x:x| server |
----------     ------------     --------------     --------------

So, the router will have to be configured separately with port forwarding rules to route inbound traffic from its public WAN IP/port to your server machine's private LAN IP/port.因此,路由器必须单独配置端口转发规则,以将入站流量从其公共 WAN IP/端口路由到服务器机器的私有 LAN IP/端口。

If the router supports uPNP and that is enabled, your server can configure those rules dynamically in code when binding/closing its listening socket.如果路由器支持 uPNP 并且已启用,则您的服务器可以在绑定/关闭其侦听套接字时在代码中动态配置这些规则。 Otherwise, you will have to configure the rules manually using your router's admin interface.否则,您将不得不使用路由器的管理界面手动配置规则。

If I'm understanding correctly, your IP address should be all you need.如果我没理解错的话,你的 IP 地址应该就是你所需要的了。 A quick Google search should be all you need to look it up.只需快速 Google 搜索即可查找。 Assuming '0.0.0.0' is your IP address, you can use that假设“0.0.0.0”是您的 IP 地址,您可以使用

暂无
暂无

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

相关问题 通过 ssh 从远程 linux 机器运行本地 python 脚本 - run local python script from remote linux machine over ssh 从本地客户端计算机向Ubuntu服务器上的Flask服务器发送和接收数据 - Send and receive data from local client machine to a flask server on an Ubuntu server 使用python将文件从本地计算机传输到远程Windows服务器 - Transfer file from local machine to remote windows server using python 如何使用远程客户端计算机连接到本地计算机 - how to connect to local machine using remote client machine 如何从远程服务器上的本地计算机运行python脚本并实时显示输出? - How to run a python script from local machine on a remote server and display the outputs live? 如何从我的本地计算机(client.py)发送一个简单的 UDP 消息到远程服务器 pythonanywhere(server.py) - How to send a simple UDP message from my local computer(client.py) to a remote server pythonanywhere(server.py) Python 将文件从远程机器移动到本地 - Python Move a File from Remote Machine to Local 如何在远程机器上运行本地python脚本 - how to run local python script on remote machine 使用python脚本从远程服务器到同一远程服务器的mysqldump来自本地机器! - mysqldump from remote server to same remote serveritself from a local machine using python script! 在远程机器上使用 Paramiko 通过 sshClient() 执行本地 python 脚本 - execute local python script over sshClient() with Paramiko in remote machine
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM