更新时间:2023-12-05 09:48:34
如果你已经有了一个现有的网站
您无法直接从网页执行服务器端Python脚本(而不是执行客户端Python脚本的话)。你必须有服务器拦截请求,然后让它执行python脚本。
You can't directly execute server-side python scripts from a web page (and not execute client side python scripts at all). You'll have to have the server intercept the request, and then make it execute the python script.
也就是说,你需要一个管道,看起来像这样:
That is, you need a pipeline that looks like this:
[ client browser ] ----> [ web server ]
[ client browser ] [ web server ] ----> [ python myscript.py ]
[ client browser ] [ web server ] <---- [ python myscript.py ]
[ client browser ] <---- [ web server ]
您可以编写自己的Web服务器软件,它会做到这一点,但如果你真的只是有一个python脚本,CGI常被用来让用户运行随心所欲的服务器端脚本,并接受标准输出
输出结果。
You can write your own web server software that'll do this, but if you really just have a python script, CGI is often used to let users run "arbitrary" server side scripts, and receive the stdout
output as a result.
根据您选择的Web服务器的,有做不同的方式,但这里的基础蟒蛇页面应该有希望给你足够的关键字找到适合你的环境的解决方案。你会发现导游的负荷,如果你谷歌搜索$ myhttpservername蟒蛇CGI。
Depending on your choice of web server, there's different ways of doing it, but here's the base python page that should hopefully give you enough keywords to find a solution that fits your environment. You'll find loads of guides if you search Google for "$myhttpservername python CGI".
https://docs.python.org/2/howto/webservers.html
请参阅下面的指导,就如何建立一个python的cgi-bin在Apache:
Please see the following guide, on how to set up a python cgi-bin on Apache:
http://www.linux.com/community/blogs/129-servers/757148-configuring-apache2-to-run-python-scripts
如果你没有一个Web服务器,只是想提供一个python脚本
有一个不同的方法是在承载Web服务器本身就是一个python脚本。其中一个例子是 CherryPy的。这个例子说明如何在8080端口创建一个web服务器:
A different approach would be to create a python script that hosts the web server itself. One such example is cherrypy. The example shows how to create a web server on port 8080:
import cherrypy
class HelloWorld(object):
def index(self):
return "Hello World!" # or whatever you want to do
index.exposed = True
cherrypy.quickstart(HelloWorld())