使用浏览器运行命令

发布于 2024-12-09 16:44:04 字数 264 浏览 0 评论 0原文

我想在网站上有一个“控制面板”,当按下按钮时,我希望它在服务器(我的计算机)上运行命令。该面板用于运行我编写的不同 python 脚本(每个按钮一个脚本),我想在我的 Mac、我的 iPod touch 和我的 Wii 上运行该面板。我认为最好的方法是建立一个网站,因为它们都有浏览器。每当按下按钮时,是否有 JavaScript 或其他东西可以在我的计算机上运行命令?

编辑:我听说 AJAX 可能适用于像这样的基于服务器的事情,但我不知道如何做到这一点。是否有类似“系统”块或我可以使用的东西?

I want to have a "control panel" on a website, and when a button is pressed, I want it to run a command on the server (my computer). The panel is to run different python scripts I wrote (one script for each button), and I want to run the panel on my Mac, my iPod touch, and my wii. The best way I see for this is a website, since they all have browsers. Is there a javascript or something to run a command on my computer whenever the button is pressed?

EDIT: I heard AJAX might work for server-based things like this, but I have no idea how to do that. Is there like a 'system' block or something I can use?

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

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

发布评论

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

评论(3

兮子 2024-12-16 16:44:04

这里有三个选项:

  1. 让每个按钮提交一个表单,并在隐藏字段中包含脚本的名称。服务器将接收表单参数,然后可以分支以运行适当的脚本。
  2. 让每个按钮挂钩到它自己的唯一 URL,并在按钮单击时使用 javascript 将 window.location 设置为该新 URL。您的服务器将收到该 URL,并可以根据该 URL 决定运行哪个脚本。您甚至可以只使用网页上的链接,而不使用 JavaScript。
  3. 使用 Ajax 向您的服务器发出唯一的 URL。这与前两个选项本质上相同(从服务器的角度来看)。主要区别在于 Web 浏览器不会更改其指向的 URL。 ajax 调用只是指示服务器执行某些操作并返回一些数据,然后主机网页可以执行任何它想要的操作。

Here are three options:

  1. Have each button submit a form with the name of the script in a hidden field. The server will receive the form parameters and can then branch off to run the appropriate script.
  2. Have each button hooked to it's own unique URL and use javascript on the button click to just set window.location to that new URL. Your server will receive that URL and can decide which script to run based on the URL. You could even just use a link on the web page with no javascript.
  3. Use Ajax to issue a unique URL to your server. This is essentially the same (from the server's point of view) as the previous two options. The main difference is that the web browser doesn't change what URL it's pointing to. The ajax call just directs the server to do something and return some data which the host web page can then do whatever it wants with.
み零 2024-12-16 16:44:04

在客户端(浏览器),您可以使用最简单的方法来完成。只是一个 html 表单。 javascript 可以更好地进行验证并进行 ajax 调用,这样页面就不必刷新。但您的主要重点是在服务器上处理它。您可以收到采用您选择的语言的表格请求。如果你已经在运行 python,你可以编写一个超快的 cgi python 脚本。查看 python 的 cgi 模块。如果您将在 osx 上托管它,则需要将其放入 osx 上的 apache 服务器中。
不幸的是,您关于如何编写它的问题超出了简单答案的范围。但是谷歌搜索如何编写 html 表单,或者查看 jquery 来构建一个可以轻松进行 ajax 调用的快速表单。
然后搜索如何使用python cgi模块并接收POST请求。

On the client side (the browser), you can do it with the simplest approach. Just an html form. javascript would make it nicer for validation and to do ajax calls so the page doesnt have to refresh. But your main focus is handling it on the server. You could receive the form request in the language of your choice. If you are already running python, you could write a super fast cgi python script. Look at the cgi module for python. You would need to put this into the apache server on osx if thats where you will host it.
Unfortunately, your question about exactly how to write it is beyond the scope of a simple answer. But google for how to write and html form, or look at maybe jquery to build a quick form that can make ajax calls easily.
Then search for how to use the python cgi module and receive POST requests.

笑,眼淚并存 2024-12-16 16:44:04

Javascript 基本上用于在浏览器中完成工作(通常是呈现一些不错的内容供最终用户查看)。您想要的(正如其他人已经说过的那样)是将 HTML 表单操作连接到网络服务器“后端”上的操作的方法。这正是(正如 RobG 所指出的)CGI 的用途。受 Apache 用户欢迎的 CGI 替代方案是 mod_python - 区别基本上在于“后端”操作是作为独立进程 (CGI) 还是在 Web 服务器进程 (mod_python) 内运行,但对于大多数基本应用程序,您的服务器端脚本不需要关心。如果您处于共享托管环境中,您可能别无选择 - 请询问您的系统管理员(或阅读您的托管服务文档)以了解在这种情况下如何最好地运行 CGI 脚本。

注意事项:

  1. 您可能需要相当高的网络服务器管理员访问权限和权限。专业知识,以便按照您想要的方式设置一切。您至少需要能够(在权限和技术理解方面)查看您的网络服务器日志、编辑您的网络服务器配置并反弹(重新启动)您的 http 服务。

  2. 无论您想要完成什么“后端”操作,都将使用网络服务器的权限/特权来完成,这可能与您通常用于执行这些操作的用户帐户的权限/特权不同。有多种方法可以解决这个问题(使用自定义守护进程和/或 sudo 操作),但您确实需要与网络服务器系统管理员(如果网络服务器暴露在大坏互联网上)清楚地了解这是如何工作的<在部署任何内容之前,否则您将面临非常现实的风险(尤其是如果您是菜鸟),黑客可能会利用您的“命令网关”来攻击网络服务器.

当然,如果您只是为了在个人笔记本电脑上进行所有这些操作(毕竟问题上有一个 OSX 标签),那么就是网络服务器系统管理员,并且您可以自由地黑客攻击并愉快地反复搬起石头砸自己的脚,同时学习你需要知道的一切,只要你不在网络上就可以。在这种情况下,您可能会发现本教程很有用。

Javascript is basically for doing work in the browser (usually to render something nice for the end user to look at). What you want (as others have said already) is a way to connect an HTML form action to an action on the webserver "back end". And this is exactly (as RobG has pointed out) what CGI is for. An alternative to CGI which is quite popular with Apache users is mod_python - the difference is basically whether the "back end" operation runs as a standalone process (CGI) or inside a webserver process (mod_python), but for most basic applications your server side scripts don't need to care. And if you're in a shared hosting environment you may not have a choice - ask your sysadmin (or read your hosting service docs) to learn how best to run CGI scripts in this case.

Caveats:

  1. You will probably need fairly elevated webserver admin access & expertise in order to get everything set up the way you want. You will at least need to be able (both in the sense of permissions and technical understanding) to view your webserver logs, edit your webserver configs and bounce (restart) your http service.

  2. Whatever "back end" operations you want done will be done with the permissions/privileges of the webserver, which may not be the same as the permissions/privileges of the user account which you normally use to perform these operations. There are various ways around this (using custom daemons and/or sudo operations), but you really need to have a clear understanding with the webserver sysadmin (if the webserver is exposed to the Big Bad Internet) about how this is going to work before you deploy anything, otherwise you run the very real risk (especially if you are a noob) of making it possible for hackers to exploit your "command gateway" to hack the webserver.

Of course if you're just doing all this for fun on your personal laptop (there is an OSX tag on the question, after all), then you are the webserver sysadmin, and you're free to hack away and happily shoot yourself in the foot repeatedly while learning everything you need to know along the way, which is fine as long as you're not on a network. In this case, you may find this tutorial to be useful.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文