HTTP使用React从Python Echo服务器获取请求

发布于 2025-01-27 03:53:29 字数 1283 浏览 2 评论 0原文

我有一台Python Echo服务器,当客户端发送GET请求时,它会发送连续的数据流,如下所示:

GET request for /?value=30
GET request for /?value=30
GET request for /?value=30
GET request for /?value=37
GET request for /?value=37
GET request for /?value=37

Python Echo Server的摘要,该echo Echo服务器使用http.server库:

from http.server import BaseHTTPRequestHandler, HTTPServer
import logging


class S(BaseHTTPRequestHandler):
    def _set_response(self):
        self.send_response(200)
        self.send_header('Content-type', 'text/html')
        self.end_headers()

    def do_GET(self):
        logging.info("GET request,\nPath: %s\nHeaders:\n%s\n", str(text), str(self.headers))
        self._set_response()
        self.wfile.write("get_value: GET request for {}".format(text).encode('utf-8'))

node.js code:node.js代码,该代码在上面记录了数据响应:

var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;

function httpGet(theUrl) {
  let xmlHttpReq = new XMLHttpRequest();
  xmlHttpReq.open("GET", theUrl, false); 
  xmlHttpReq.send(null);
  return xmlHttpReq.responseText;
}
var i = 1
while (i ==1) {
  console.log(httpGet('http://localhost:8080'));
}

我已经成功地使用了node.js代码检索了数据,但是我在React中没有成功实现它。我应该如何在React中编写简单的获取/提取请求?我非常新手反应,感谢您的指导。

I have a Python echo server that sends a continuous stream of data when a client sends a GET request, as follows:

GET request for /?value=30
GET request for /?value=30
GET request for /?value=30
GET request for /?value=37
GET request for /?value=37
GET request for /?value=37

The snippet of the Python echo server, which uses the http.server library:

from http.server import BaseHTTPRequestHandler, HTTPServer
import logging


class S(BaseHTTPRequestHandler):
    def _set_response(self):
        self.send_response(200)
        self.send_header('Content-type', 'text/html')
        self.end_headers()

    def do_GET(self):
        logging.info("GET request,\nPath: %s\nHeaders:\n%s\n", str(text), str(self.headers))
        self._set_response()
        self.wfile.write("get_value: GET request for {}".format(text).encode('utf-8'))

Node.js code that logged the data response above:

var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;

function httpGet(theUrl) {
  let xmlHttpReq = new XMLHttpRequest();
  xmlHttpReq.open("GET", theUrl, false); 
  xmlHttpReq.send(null);
  return xmlHttpReq.responseText;
}
var i = 1
while (i ==1) {
  console.log(httpGet('http://localhost:8080'));
}

I have successfully retrieved the data using a Node.js code however I wasn't successful in implementing it in React. How should I write a simple GET/fetch request in React? I'm very new to React and thanks for your guidance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

葵雨 2025-02-03 03:53:29

您可以使用Fetch-API创建HTTP请求,以下是React示例:

componentDidMount() { 
   fetch('http://localhost:8080') 
     .then((res) => res.json()  {
      console.log(res)
      }) 
}

You can use the fetch-api to create HTTP request, here is react example:

componentDidMount() { 
   fetch('http://localhost:8080') 
     .then((res) => res.json()  {
      console.log(res)
      }) 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文