CGI 和 Python - 将选择返回给 python 脚本

发布于 2024-07-18 01:41:55 字数 678 浏览 5 评论 0原文

我有一个 python 脚本,一旦从命令行执行,就会执行所需的操作并退出。 如果在执行过程中,程序无法执行选择,他会提示用户并要求他们做出决定!

现在我必须实现一个 Web 界面,问题来了...我创建了一个带有简单表单的 htm 文件,一旦用户“提交”,他就会将参数传递给仅包含一行并运行我的 cgi 脚本。蟒蛇程序! 这似乎有效。

我的问题是:如果程序需要询问用户选择,我怎样才能将该值返回到我的python脚本? 为了提示用户做出选择,我需要创建一个包含可能选择的网页...有人知道如何使用 python 打开网页吗?

第二个也是最重要的问题是:如何将网页中的值返回到我的“原始”python 模块? 在 python 中,我会简单地制作一个

return choice

,但对于网页,我不知道该怎么做。

回顾:

  1. 从网页开始,我运行一个 CGI 脚本! 完成

  2. 这个 CGI 脚本运行我的 python 程序...完成

  3. 如果程序无法做出决定,

    3a 创建一个网页,其中包含我可以做的可能选择

    3b 显示创建的网页?????????

    3c 将响应返回到原始 python 模块?????????

I have a python script that, once executed from command line, performs the needed operations and exit. If, during the execution, the program is not able to perform a choice, he prompts the user and asks them to take a decision!

Now I have to implement a web interface, and here comes the problems ... I created an htm file with a simple form that, once the user "submits" he passes the parameters to a cgi script that contains just one line and runs my python program ! And it seems to work.

My question is: if it happens that the program needs to ask the user for a choice, how can I return this value to my python script? To prompt the user for a choice I need to create a webpage with the possible choices ... Does anybody know how can I open a webpage with python ?

The second and most important question is: how can I return a value from a web page to my "original" python module? In python I would simply make a

return choice

but with a web page I have no idea how to do it.

Recap:

  1. Starting from a web page, I run a cgi script ! Done

  2. This CGI script runs my python program... Done

  3. If the program is not able to take a decision,

    3a create a web page with the possible choices I can do it

    3b display the created web page ????????

    3c return the response to the original python module ????????

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

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

发布评论

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

评论(5

久光 2024-07-25 01:41:55

“有人知道如何使用 python 打开网页吗?第二个也是最重要的问题是:如何将网页中的值返回到我的“原始”python 模块??”

这一切都很简单。

但是,您需要了解网络的真正含义。 您需要阅读有关 Web 服务器、浏览器和 HTTP 协议的内容。

这是黄金法则:网络服务器通过网页响应 HTTP 请求。

该规则的第二部分是:请求是一个 URL 和一个方法(GET 或 POST)。< /strong> 请求还有更多内容,但这是重要的部分。

这就是所发生的一切。 因此,您必须将用例重新转换为上述形式。

人点击书签; 浏览器发出一个空请求(到“/”的 URL)并获取一个表单。

人员填写表格,点击按钮; 浏览器 POST 请求(到表单中的 URL)并获取两件事之一。

  • 如果您的脚本有效,他们会得到表明一切有效的页面。

  • 如果您的脚本需要信息,他们会获得另一种形式。

人员填写表格,点击按钮; 浏览器 POST 请求(发送到表单中的 URL)并获取表明一切正常的最终页面。

您可以通过“CGI”脚本完成所有这些操作。 使用 mod_wsgi 并将您的内容插入 Apache Web 服务器。

或者,您可以获得一个网络框架。 Django、TurboGears、web.py 等。即使您认为操作很简单,您也会对框架感到更满意。

"Does anybody know how can I open a webpage with python ? The second and most important question is: how can I return a value from a web page to my "original" python module ??"

This is all very simple.

However, you need to read about what the web really is. You need to read up on web servers, browsers and the HTTP protocol.

Here's the golden rule: A web server responds to HTTP requests with a web page.

The second part of that rules is: A Request is a URL and a method (GET or POST). There's more to a request, but that's the important part.

That's all that ever happens. So, you have to recast your use case into the above form.

Person clicks a bookmark; browser makes an empty request (to a URL of "/") and gets a form.

Person fills in the form, clicks the button; browser POST's the request (to the URL in the form) and gets one of two things.

  • If your script worked, they get their page that says it all worked.

  • If your script needed information, they get another form.

Person fills in the form, clicks the button; browser POST's the request (to the URL in the form) and gets the final page that says it all worked.

You can do all of this from a "CGI" script. Use mod_wsgi and plug your stuff into the Apache web server.

Or, you can get a web framework. Django, TurboGears, web.py, etc. You'll be happier with a framework even though you think your operation is simple.

南…巷孤猫 2024-07-25 01:41:55

我认为如果需要选择并接受选择作为参数,您可以修改 Python 脚本以返回错误。 如果这样做,您可以检查 cgi 脚本的返回值,并使用它适当地调用 python 脚本并将信息返回给用户。

有什么原因不能直接调用python脚本吗? 我怀疑如果您避免中间的 CGI,您最终会得到一个更简洁的实现。

您使用什么网络服务器? 什么cgi语言? 也许是珀尔?

I think you could modify the Python script to return an error if it needs a choice and accept choices as arguments. If you do that, you can check the return value from your cgi script and use that to call the python script appropriately and return the information to the user.

Is there a reason why you can't call the python script directly? I suspect you'd end up with a neater implementation if you were to avoid the intermediate CGI.

What webserver are you using? What cgi language? Perl maybe?

帅哥哥的热头脑 2024-07-25 01:41:55

网页不返回值,也不是程序 - 网页只是 HTML 或浏览器可以显示的类似内容的静态集合。 您的 CGI 脚本不能等待用户发送响应 - 它必须将网页发送给用户并终止。

但是,如果浏览器根据该页面中的数据对您的 CGI 程序(或不同的 CGI 程序)执行第二次查询,那么您可以通过这种方式收集信息并从该点继续。

Web pages don't return values, and they aren't programs - a web page is just a static collection of HTML or something similar which a browser can display. Your CGI script can't wait for the user to send a response - it must send the web page to the user and terminate.

However, if the browser performs a second query to your CGI program (or a different CGI program) based on the data in that page, then you can collect the information that way and continue from that point.

白日梦 2024-07-25 01:41:55

如果您用 python 编写 cgi,然后从 cgi 脚本调用 python 脚本,可能会更容易。

更新脚本以将 UI 与逻辑分开。
然后,将脚本与 (python) cgi 脚本连接起来应该相对容易。

对于 python cgi 参考:
五分钟了解 Python CGI

Probably easier if you write your cgi in python then call your python script from the cgi script.

Update your script to separate the UI from the logic.
Then it should be relatively easy to interface your script with the (python) cgi script.

For python cgi reference:
Five minutes to a Python CGI

鹤舞 2024-07-25 01:41:55

http://docs.python.org/library/cgihttpserver.html

我认为首先您需要将代码与界面分开。 当您运行脚本时,它会输出一个页面。 您可以使用 url 参数向其传递参数。 理想情况下,您想要执行逻辑,然后将结果传递到 python 打印到 cgi 的模板中。

http://docs.python.org/library/cgihttpserver.html

I think first off you need to separate your code from your interface. When you run a script, it spits out a page. You can pass arguments to it using url parameters. Ideally you want to do your logic, and then pass the results into a template that python prints to the cgi.

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