使用 python 为基于 Web 的 ubuntu 终端编写伪终端
我有兴趣编写一个基于网络的终端,它可以在 ubuntu 终端中执行和自动完成命令。
我摆弄了 Anyterm http://www.anyterm.org 它使用 ROTE:“一个用于 VT102 的简单 C 库后端服务器上的“终端仿真”和终端 ui 上的 javascript/html。问题是我无法让它在同一个窗口中打开两个终端。
经过一些研究后,我决定用 python 编写一个终端服务器/伪终端(以正确的术语为准),它可以:
- 在 ubuntu 终端中执行命令(例如 ls、grep 等...)
- 自动完成命令(例如 $ gedi(按下选项卡)$gedit)
- 并同时打开多个终端实例(当在一个终端中键入 ls 时,另一个终端不会将 ls 识别为自另一个终端会话以来的最后一个命令)
我发现使用 <一个href="http://docs.python.org/release/3.2.2/library/pty.html#module-pty" rel="nofollow">pty python module 我可以写一个伪终端 但是我是 python 新手,我什至无法让该页面上的示例正常工作。
我计划让 python 脚本通过我编写的 javascript/html 终端 ui 为 http 服务器提供服务(这是我想为这个项目选择 python 的主要原因)。然后我想让 Web ui 与后端终端“服务器”对话,并将结果返回给 Web ui。
如果你能指出正确的方向,也许告诉我应该使用哪个模块 - 如果不是 pty - 并给我一些关于如何编写 python 伪终端服务器的想法,我将不胜感激。
我了解 javascript/php 并熟悉 bash 脚本 - 此时我可以选择基于 C 或基于 python 的后端服务器,python 适合这个项目吗?
注意:我计划编写一个使用 ajax post 或 get 方法从后端服务器检索终端输出的用户界面。
I am interested in writing a web based terminal which can execute and autocomplete the commands in ubuntu terminal.
I have fiddled with Anyterm http://www.anyterm.org which uses ROTE: "a simple C library for VT102 terminal emulation" at the backend server and javascript/html for the terminal ui. The problem was that I couldn't have it open two terminals in the same window.
After doing some research I've decided to write a terminal server/pseudo terminal (whichever is the right terminology) in python which can:
- execute commands in ubuntu terminal (such as ls, grep etc...)
- autocomplete the commands (like $gedi (pressed tab) $gedit)
- and have multiple instances of terminal open at the same time (when typed ls in one, the other terminal won't recognize ls as the last command since its another terminal session)
I have found that with pty python module I can write a pseudo terminal
however I'm new to python and I couldn't even get the example on that page to work.
I'm planning to have the python script serve a http server with the javascript/html terminal ui I would write (this was the main reason I wanted to choose python for this project). Then I want to let the web ui talk to the backend terminal "server" and get the results back to the web ui.
If you could point me in the right direction, maybe tell me which module I should use -if not pty- and give me some ideas on how to write the python pseudo terminal server I would appreciate it.
I know javascript/php and familiar with bash scripting- At this point I could go for a C based or python based backend server, is python right for this project?
Note: I'm planning to write a ui that uses ajax post or get methods to retrieve the terminal output from backend server.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要首先学习基本术语。 shell 是解释输入命令行的程序,例如 grep "foo" abc.txt。 终端模拟器是模仿终端的程序。终端是一种带有显示屏和键盘的设备
20 世纪 70 年代用于访问 UNIX 大型机。 伪终端,pty是支持终端仿真器的设备节点,与“真实终端设备”(tty1) 等不同。
现在,您可以构建一个 shell,它可以在终端而不是 bash 或 dash 中运行;或者您可以构建一个可通过互联网使用的终端模拟器;您可以在其中运行任何现有的 shell。两者本身都是具有挑战性的任务。
Python 文档没有多大用处,我建议您应该首先通过谷歌搜索更多这些概念 - 不仅是 Python 参考资料,还有 C、C++ 参考资料。
最后,如果您想在 shell 中运行 Emacs 或 nano 或其他一些高级程序,您需要使用 pty 模块。
You need to learn the basic terminology first. The shell is the program that interprets your input command lines, such as grep "foo" abc.txt. A terminal emulator is a program that mimics a terminal. Terminal is a device with display and keyboard that one
used in 1970s to access a UNIX mainframe. A pseudoterminal, pty is the device node supporting terminal emulators, as opposed to "real terminal devices" (tty1) for example.
Now, you could either build a shell, that would work in terminal instead of bash or dash; or you could build a terminal emulator that is usable over the internet; you could run any existing shell in it. Both are challenging tasks by themselves.
Python documentation is not of much use, I recommend you should start by googling more of these concepts first - and not only python references, but C, C++ too.
And lastly, if you want to run Emacs or nano or some other advanced program in your shell, you want to use the pty module.
我有一个朋友做了类似的事情,但他是通过 Google 聊天完成的,我不知道它会有多大帮助,但看看 xmpp (python modual) 也许它可以帮助你,甚至这里有一个链接他的源代码:
chatIO
我没有和他一起合作,但他使用起来真的很容易
祝你好运
I had a friends who did something similar but he did it over Google chat, I don't know how much help it will be but take a look at xmpp (python modual) maybe it could help you, or even here is a link to his source code:
chatIO
I didn't work with him on it but it was really easy for him to use
Good Luck