从服务器传输数据到远程个人计算机。

huangapple go评论65阅读模式
英文:

transfer data from server to remote personal computer

问题

Here is the translated content:

我有一个 Node.js 后端,并想通过 API 将数据发送到运行着用 Python 编写的应用程序的远程计算机,以接收该数据。我在客户端使用以下代码:

from flask import Flask, jsonify, request

app = Flask(__name__)  # 创建一个新的 Flask 应用实例

@app.route('/api', methods=['GET'])  # 定义 API 端点的路由
def get_data():
    return jsonify({'message': 'Hello from the API!'})  # 返回带有消息的 JSON 响应

if __name__ == '__main__':
    app.run(host='192.168.x.x', debug=True)

以及在服务器端使用 Node.js 的以下代码:

const axios = require('axios');

axios.get('http://192.168.x.x:5000/api')
.then(response => {
console.log(response.data);
})
.catch(error => {
console.error(error);
});

当我在同一台机器上运行这两个代码时,它正常工作,但当我在一台机器上运行 Python 代码,另一台机器上运行 Node.js 代码时,我收到以下错误:

cause: Error: connect ECONNREFUSED 192.168.x.x:5000
  at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1247:16) {
errno: -4078,
code: 'ECONNREFUSED',
syscall: 'connect',
address: '192.168.x.x',
port: 5000
}

在我来这里之前,我使用了 ChatGPT,在 YouTube 上观看了一些视频,但没有帮助。我也是网络方面的初学者。如果在客户端创建 API 是愚蠢的,我该怎么做?尝试切换到套接字编程吗?

我感激任何建议或回应。

注意:
192.168.x.x 是我的以太网适配器 Ethernet 3 的 IPv4 地址。
我希望从在单独计算机上运行的 Node.js 代码中接收数据。

英文:

I have a Node.js backend and want to send data through an API to a remote computer which run an app which wrote in python to recieve that data. I use this code in client side:

from flask import Flask, jsonify, request

app = Flask(__name__)  # Create a new Flask app instance

@app.route('/api', methods=['GET'])  # Define a route for the API endpoint
def get_data():
    return jsonify({'message': 'Hello from the API!'})  # Return a JSON response with a message

if __name__ == '__main__':
    app.run(host='192.168.x.x',debug=True)  

and this in server side using Node.js:

const axios = require('axios');

axios.get('http://192.168.x.x:5000/api')
.then(response => {
console.log(response.data);
})
.catch(error => {
console.error(error);
});

when I run these two codes on a same machine it works properly but when I run python code on a machine and Node.js code on another, and I get this error:

cause: Error: connect ECONNREFUSED 192.168.x.x:5000   
  at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1247:16) {
errno: -4078,
code: 'ECONNREFUSED',
syscall: 'connect',
address: '192.168.x.x',
port: 5000

}

Before I came here I use ChatGPT, watch some videos on YouTube but it doesn't help.Also I am beginner in network. If it is stupid to create api in client side what I have to do? trying to switch to socket programming ?

I appreciate any advice or response.

note:
192.168.x.x is my Ethernet adapter Ethernet 3 IPv4 Address.
I expect that recive data from Nodejs code which run on a seperate computer.

答案1

得分: 1

Here are the translated parts:

  1. If it is stupid to create api in client side what I have to do? trying to switch to socket programming?

    • 如果在客户端创建API是愚蠢的,我该怎么办?尝试切换到套接字编程吗?
  2. I have a nodejs server, and the client will download a python app and run it on its machine. To transfer data from that server to that application on the client side, I use API.

    • 我有一个Node.js服务器,客户端将下载一个Python应用程序并在其机器上运行它。为了将数据从该服务器传输到客户端上的该应用程序,我使用API。
  3. Any "server" (i.e., listens to an open port, HTTP, or otherwise) will need to be on an IP that can be reached by its "clients" (connect to that listening port). If they're on the same local network, their IPs are routable and, firewalls notwithstanding, they can connect to each other. If they're on different local networks, perhaps connected by the internet, then their local IPs are not routable, and they cannot connect to each other. So for example, if I'm at my house and you're at your house, we don't share a local network, so we can't connect to each other's local IPs.

    • 任何“服务器”(即监听开放端口,HTTP或其他方式)都需要位于可以被其“客户端”访问的IP上(连接到该监听端口)。如果它们位于同一本地网络上,它们的IP是可路由的,除防火墙外,它们可以互相连接。如果它们位于不同的本地网络上,可能通过互联网连接,那么它们的本地IP不可路由,它们无法互相连接。例如,如果我在我的房子里,而你在你的房子里,我们不共享本地网络,所以我们不能连接到彼此的本地IP。
  4. In the context of web hosting environments, a server would have a public IP assigned to it. Anything connected to the internet could reach the public IP and make - in your case - http requests.

    • 在Web托管环境中,服务器将被分配一个公共IP。与互联网连接的任何设备都可以访问公共IP,并进行HTTP请求(在你的情况下)。
  5. In the context of home environments, your internet router holds a public IP address and does what's called Network Address Translation, which is a fancy way of saying it rewrites the source IP of outgoing traffic to be its own public IP. The remote recipient of the traffic responds to the public IP of the router, which then rewrites the destination IP back to the original private source, to which it then forwards the request. This is how very nearly all private networks at everybody's house, office, etc., will be set up (and the reason IPv4 address space exhaustion never really became a huge problem).

    • 在家庭环境中,您的互联网路由器持有一个公共IP地址,并执行所谓的网络地址转换,这是一种将出站流量的源IP重写为其自身公共IP的高级方式。流量的远程接收方会响应路由器的公共IP,然后将目标IP重写回原始私有源IP,然后将请求转发。这几乎是每个人家、办公室等私有网络都设置的方式(也是IPv4地址空间耗尽从未真正成为一个巨大问题的原因)。
  6. To be able to connect to a machine behind a NAT router, you can't use the private IP in the remote network. It is not routable through the internet (private networks share IP space, so they cannot be internet routed). You have to use the NAT router's Public IP space. So you have to log into the NAT router's admin interface and configure it so that incoming connections to a particular port are forwarded to a particular private IP, which then must be static lest the configuration become outdated.

    • 要能够连接到NAT路由器后面的机器,您不能使用远程网络中的私有IP。它不能通过互联网路由(私有网络共享IP空间,因此不能通过互联网路由)。您必须使用NAT路由器的公共IP空间。因此,您必须登录NAT路由器的管理界面,并配置它,以便将到达特定端口的入站连接转发到特定的私有IP,这个私有IP必须是静态的,以免配置过时。
  7. For clients to run a service that will listen for connections, they will also have to configure their home router to forward those connections and keep that configuration up to date. This is why it is all but unheard of for "clients" to also open listening ports in current times. Even if all your clients are power users who can make the necessary configurations, there's really no benefit to it. If you have a webserver, for example, you can GET or DELETE data from the server, or POST/PUT/PATCH data to the server. There's really no reason for the client to also listen on a port.

    • 为了使客户端运行一个可以监听连接的服务,他们还必须配置他们的家用路由器以转发这些连接并保持配置最新。这就是为什么在当前时代几乎没有听说过“客户端”也打开监听端口的原因。即使所有客户端都是高级用户,可以进行必要的配置,实际上也没有什么好处。例如,如果您有一个Web服务器,您可以从服务器获取或删除数据,或者将数据POST/PUT/PATCH到服务器。客户端实际上没有理由也监听一个端口。
  8. client will download a python app

    • 客户端将下载一个Python应用程序
  9. Remember that to run the python app, the client will also need to download and install a python runtime as well as any required dependencies.

    • 请记住,要运行Python应用程序,客户端还需要下载并安装Python运行时以及所有必需的依赖项。
  10. Distributing software is actually pretty difficult. Even if everybody already has python installed and you use nothing but the standard library (which does not include your

英文:

> If it is stupid to create api in client side what I have to do? trying to switch to socket programming ?

> I have a nodejs server ,and client will download a python app and run it on its machine. .to transfer data from that server to that application on client side I use API.

any "server" (ie listens to open port, http or otherwise) will need to be on an IP that can be reached by its "clients" (connect to that listening port). if they're on the same local network, their IPs are routable and, firewalls notwithstanding, they can connect to each other. If they're on different local networks, perhaps connected by the internet, then their local IPs are not routable and they cannot connect to each other. So for example, if I'm at my house and you're at your house, we don't share a local network, so we can't connect to each other's local IPs.

In the context of web hosting environments, a server would have a public IP assigned to it. Anything connected to the internet could reach the public IP and make - in your case - http requests.

In the context of home environments. your internet router holds a public IP address and does what's called Network Address Translation, which is a fancy way of saying it rewrites the source IP of outgoing traffic to be its own public IP. The remote recipient of the traffic responds to the public IP of the router, which then rewrites the destination IP back to the original private source, to which it then forwards the request. This is how very nearly all private networks at everybody's house, office, etc will be set up (and the reason IPv4 address space exhaustion never really became a huge problem).

To be able to connect to a machine behind a NAT router, you can't use the private IP in the remote network. It is not routable through the internet (private networks share IP space so they cannot be internet routed). You have to use the NAT router's Public IP space. So you have to log into the NAT router's admin interface and configure it so that incoming connections to a particular port are forward to a particular private IP, which then must be static lest the configuration become outdated.

For clients to run a service that will listen for connections, they will also have to configure their home router to forward those connections and keep that configuration up to date.

This is why it is all but unheard of for "clients" to also open listening ports in current times.

Even if all your clients are power users who can make the necessary configurations, there's really no benefit to it. If you have a webserver, for example, you can GET or DELETE data from the server, or POST/PUT/PATCH data to the server. There's really no reason for the client to also listen on a port.

> client will download a python app

Remember that to run the python app, the client will also need to download and install a python runtime as well as any required dependencies.

Distributing software is actually pretty difficult. Even if everybody already has python installed and you use nothing but the standard library (which does not include your dependencies), you'll also need to update their versions of the code when you find the next bug.

This consideration makes browser centric model of application development very appealing. You don't distribute code (complicated, dangerous) you distribute web URLs. The client simply enters the URL into a trusted browser, and all requisite code (in the form of javascript) and the rest of the web assets are automatically retrieved and executed. Nothing needs to be permanently installed locally. The entire application runs in a well defined sandbox, so the client can rest assured it is much safer.

> Also I am beginner in network.

Everything you're doing is technically possible. You could get all clients to configure their local networks and their systems, download and execute your code. You can write programs that implement all sorts of strange network behaviors. Writing client/server apps is a great way to get to know network programming. But it's a poor way to deliver functionality to clients. Incidentally, web application development doesn't teach you much about network programming anyway - once of its appeals is abstraction around the underlying network layers.

In addition, I strongly advise against distributing client code when at all possible. Even if you end up wanting to make mobile apps, they often work almost the same as websites, so your experience with the web's programming model will serve you well if you end up doing this kind of thing for a living.

huangapple
  • 本文由 发表于 2023年4月17日 05:36:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76030449.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定