[英]Configure apache to run python (cgi)
i've wampserver with apache 2.4.4 installed in 我已经在安装了Apache 2.4.4的wampserver中
I've installed python and i created a test file : 我已经安装了python并且创建了一个测试文件:
#!/Python34/python
print "Content-type: text/html"
print
print "<html><head>"
print ""
print "</head><body>"
print "Hello."
print "</body></html>"
i wanna know how to run this script ? 我想知道如何运行此脚本?
I personally don't like the way CGI and all this stuff work (slow to spawn processes, need to use some kind of tricks like "fastcgi" to bypass this, etc...) 我个人不喜欢CGI和所有这些东西的工作方式(缓慢生成进程,需要使用诸如“ fastcgi”之类的技巧来绕开它,等等。)
I think you may build your Python programm as an HTTP server (Use cherrypy for example, or whatever you want.), start your Python program to listen on localhost:whatever, then, from the Apache side, just configure a proxy to localhost:whatever. 我认为您可以将Python程序构建为HTTP服务器(例如,使用cherrypy或您想要的任何东西),启动Python程序以监听localhost:无论如何,然后从Apache端,只需将代理配置为localhost:随你。
Pros: 优点:
Configuring apache 2 to pass your requests to your python daemon is as easy as: 配置apache 2将您的请求传递给python守护程序很简单:
<VirtualHost *:80>
ServerName example.com
ProxyPass / http://localhost:8080/
</VirtualHost>
And an hello world from the cherrypy documentation: 以及来自cherrypy文档的世界:
import cherrypy
class HelloWorld(object):
def index(self):
return "Hello World!"
index.exposed = True
cherrypy.quickstart(HelloWorld())
+1 to what Julien Palard says about not using CGI, it's really slow and inefficient. +1关于Julien Palard关于不使用CGI的说法,这确实很慢而且效率很低。 An alternative to running your server standalone and proxying through to it using Apache is to use mod_wsgi, which allows you to run Python processes inside the Apache process.
独立运行服务器并使用Apache代理服务器的另一种方法是使用mod_wsgi,它允许您在Apache进程内部运行Python进程。 Most web frameworks (Django, Bottle, Flask, CherryPy, web2py, etc) work well with it.
大多数Web框架(Django,Bottle,Flask,CherryPy,web2py等)都可以很好地工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.