如何实现“命令行界面服务器”

发布于 2025-01-13 11:00:25 字数 836 浏览 0 评论 0原文

如果我创建一个网络服务器,那么会有一个线程监听端口 8080,每次有新的 HTTP 请求时,它都会触发服务器中的一些逻辑。我想实现相同的功能,但不是在端口 8080 上侦听 HTTP 请求,而是在命令行界面上侦听来自本地操作系统 (OS) 的命令。这些命令可以来自操作系统中的任何终端,就像 HTTP 请求可以来自可以访问操作系统上端口 8080 的任何发送者一样。

我希望我能够在 Rust 中实现这个想法的与操作系统无关的实现。我确实尝试用谷歌搜索这个想法,但运气不佳;可能是因为我缺少这个想法的术语。

为了澄清一个用例:

  1. 在一个终端中,我启动“CLI 服务器”:示例:cli_server start
  2. 在另一个终端中,我向服务器发出“CLI 请求”,服务器将处理该请求并做出响应。响应应粘贴到请求终端上的 std.out 上。示例:cli_server fetch_note id-22cli_server update_note data_in_file.json

服务器应保持活动状态,直到被要求关闭。目前尚不清楚有多少独立终端会同时访问服务器。

我想我需要的是一些类似于侦听端口 8080 的代码的代码。

如果可以使答案更多,则可以将代码拆分为 cli_servercli_client简单(或可能)。

我确实知道我可以实现一个 Web 服务器并使用curl 来访问端口 8080。我准备将此作为解决方法,以防这个问题没有答案。出于好奇,我对这个问题的答案很感兴趣,也因为这对我来说是一个更简洁的解决方案。

感谢您的阅读,我希望我的问题不要太开放。

If I were creating a webserver then there would be one thread listening to port 8080 and every time there was a new HTTP request then it would trigger some logic in the server. I want to implement the same but instead of listening on port 8080 for HTTP requests, then it would listen for commands from the local operating system (OS) on a command-line interface. These commands can come from any terminal in the OS in the same way as HTTP requests can come from any sender that can access port 8080 on the OS.

I hope I am able to implement an OS-agnostic implementation of this idea in rust. I did try googling this idea without much luck; Likely because I am missing the terminology of this idea.

To clarify one use case:

  1. In one terminal I start my "CLI server": Example: cli_server start
  2. In another terminal I make a "CLI request" to the server and the server will handle the request and respond. The response should be pasted on std.out on the requesting terminal. Example: cli_server fetch_note id-22 or cli_server update_note data_in_file.json

The Server should keep alive until asked to shutdown. It is not known how many independent terminals will access the server at a time.

What I imagine I need is some code similar to the code that listens on port 8080.

It is fine to split the code in a cli_server and cli_client if that makes the answer more simple (or possible).

I do know that I could just implement a web server and use curl to hit port 8080. I am prepared for this as a workaround in case there is no answer to this question. I am interested in an answer to this question out of curiosity and also because that would be a neater solution in my case.

Thanks for reading and I hope my question is not too open ended.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文