Windows上Python与XAMPP如何结合使用?
我在我的Win7x64电脑上安装了Xampp和Python 2.7。
现在我想要利用Python语言的强大功能……我该怎么做呢?
我尝试过用mod_python和mod_wsgi,但第一个在我的Python版本上不存在,而当我安装wsgi后尝试启动Apache时却出现了错误。
< Directory "\x93C:/wsgi_app\x94"> path is invalid
我在<和'directory'之间加了一个空格,以便在这里显示这个字符串。
那么……有没有人知道有没有简单的教程来安装这些功能呢?
或者有没有人愿意一步一步地教我该怎么做?
谢谢,如果我表达得不太清楚,真抱歉。
如果你需要什么,请问我。
2 个回答
WSGI要好得多,不过我至少也在网上查了资料,尝试了好几天都没成功。CGI效率低一些,但大多数人开发时用的都是Windows,所以其实影响不大。设置起来超级简单!
CGI方法:
- 在 xampp\apache\conf\httpd.conf 文件中找到这一行:AddHandler cgi-script .cgi .pl .asp。把它改成这样:AddHandler cgi-script .cgi .pl .asp .py
- 在你创建的每个Python脚本的顶部,设置你Python的路径。例如,如果你的Python安装在C:\Python27,就写:#!/Python27/python
- 把测试的示例测试代码放在 xampp\cgi-bin 文件夹里,然后访问 localhost/cgi-bin/your-file.py
示例测试代码(根据你安装的路径修改注释中的Python路径):
#!C:/Python27/python
print "Content-type: text/html\n\n"
print "<html><head><title>Hello World from Python</title></head><body>Hello World from a Python CGI Script</body></html>"
我在 xampp 1.8.1 中测试过,如果有什么不工作,可以看看这个:
来源: http://elvenware.com/charlie/development/web/Python/Xampp.html
没错,你说得对,mod_python在Python 2.7上是无法使用的。所以mod_wsgi是最好的选择。
我推荐使用AMPPS,因为它默认启用了mod_python和Python 2.5。你可以访问AMPPS官网。
如果你还是想继续的话,
在httpd.conf文件中添加这一行:
LoadModule wsgi_module modules/mod_wsgi.so
在httpd.conf文件中取消注释这一行:
Include conf/extra/httpd-vhosts.conf
打开虚拟主机文件httpd-vhosts.conf,添加:
NameVirtualHost 127.0.0.1:80
<VirtualHost 127.0.0.1:80>
<Directory "path/to/directory/in/which/wsgi_test.wsgi/is/present">
Options FollowSymLinks Indexes
AllowOverride All
Order deny,allow
allow from All
</Directory>
ServerName 127.0.0.1
ServerAlias 127.0.0.1
WSGIScriptAlias /wsgi "path/to/wsgi_test.wsgi"
DocumentRoot "path/to/htdocs"
ErrorLog "path/to/log.err"
CustomLog "path/to/log.log" combined
</VirtualHost>
在wsgi_test.wsgi文件中添加以下几行:
def application(environ, start_response):
status = '200 OK'
output = 'Hello World!'
response_headers = [('Content-type', 'text/plain'),
('Content-Length', str(len(output)))]
start_response(status, response_headers)
return [output]
注意:不要在htdocs目录下创建测试目录。因为我还没试过这样做。这些步骤在AMPPS中对我有效。:)
然后在你喜欢的浏览器中访问127.0.0.1/wsgi,你会看到“Hello World!”。
如果没有看到,请参考快速配置指南。
或者
你可以在httpd.conf文件中添加这些行:
<IfModule wsgi_module>
<Directory path/to/directory>
Options FollowSymLinks Indexes
AllowOverride All
Order deny,allow
allow from All
</Directory>
WSGIScriptAlias /wsgi path/to/wsgi_test.wsgi
</IfModule>