简体   繁体   English

如何使用客户端javascript向具有特定端口的服务器发送请求?

[英]How to send a request using client-side javascript to a server with specific port?

The question might not describe actually my problem.这个问题可能实际上并没有描述我的问题。 So I have aa host A. I want to send a request with a payload using js in html page.所以我有一个主机 A。我想在 html 页面中使用 js 发送一个带有负载的请求。 The destination is another Host B. Host B is on another network and I want it to receive the request from A. I don't know how to do that.目的地是另一个主机 B。主机 B 在另一个网络上,我希望它接收来自 A 的请求。我不知道该怎么做。 I tried setting up a simple python server that just prints the request:我尝试设置一个简单的 python 服务器,它只打印请求:

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((localhost, PORT)) ## localhost: 127.0.0.1 and random port
s.listen()
conn, addr = s.accept()
with conn:
    print('Connected by', addr)
    data = ""
    while True:
        data += conn.recv(2048)
        if not data:
            break
    print(data)

I feel like this is very wrong.我觉得这是非常错误的。 Anyway, on AI tried to send a get request using fetch api:无论如何,在 AI 上尝试使用 fetch api 发送 get 请求:

fetch(`http://${public_ip_of_B}:${port_of_B}/${payload}`)

The request remains on pending status and throws an error.请求保持挂起状态并引发错误。

So how can I send the request from A to B?那么如何将请求从 A 发送到 B? Thanks in advance提前致谢

Don't bind to localhost .不要绑定到localhost To accept connections from anywhere, use an empty string for the host address when binding.要接受来自任何地方的连接,请在绑定时使用空字符串作为主机地址。

s.bind(('', PORT))

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

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