.py文件在浏览器中显示代码,而不是运行 [英] .py file showing code in browser instead of running

查看:368
本文介绍了.py文件在浏览器中显示代码,而不是运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试开始使用Python,但是无法为本地主机(使用Ampps)正确设置我的服务器. Python通过IDLE和命令行运行得很好,但是,当我在浏览器中打开文件时,代码将显示并且无法运行.

I'm trying to get started with Python but can't get my server setup up correctly for localhost(using Ampps). Python is running just fine through IDLE and command line, however, when I open up the file in the browser the code is displayed and not run.

我遵循了此 http://www.imladris.com/Scripts/PythonForWindows.html 教程,以获取cgi已设置,但无法正常工作.

I followed this http://www.imladris.com/Scripts/PythonForWindows.html tutorial for getting cgi set up, but it's not working.

如果有什么不同,这里是我的"hello world"程序的代码.

Here's the code for my "hello world" program, if that makes any difference.

#!/usr/bin/env python
# -*#!/usr/bin/python

print "Content-type:text/html\r\n\r\n"
print '<html>'
print '<head>'
print '<title>Hello Word - First CGI Program</title>'
print '</head>'
print '<body>'
print '<h2>Hello Word! This is my first CGI program</h2>'
print '</body>'
print '</html>'

有什么建议吗?

推荐答案

您的网络服务器将python代码视为静态文件而非可执行文件.这里的目标是您希望Apache执行python并将stdout发送回用户的浏览器.

Your web server is treating your python code as a static file rather than an executable. The goal here is that you want Apache to execute python and send the stdout back to the user's browser.

我对Ampps不熟悉,但是获得此设置的基本Apache流程是这样的.

I'm not familiar with Ampps, but the basic Apache flow to getting this setup is something like this.

  1. 编辑httpd.conf的选项"行以包含ExecCGI
  2. 通过添加以下行,使用httpd.conf作为cgi处理程序注册您的python文件:
    AddHandler .py
  3. 重新启动Apache
  4. 请确保您的shebang行(位于#!/usr/bin/env python顶部)实际上指向您的python可执行文件的路径.对于居住在C:上的python2.6,这可能显示为:
    #!\Python26\python
  5. 如果脚本必须是可移植的,则无需修改shebang行,而将ScriptInterpreterSource registry行添加到httpd.conf中,并确保Windows默认情况下使用Python.exe打开* .py文件.可以通过双击脚本进行测试.如果它没有执行,请右键单击它,选择打开方式,然后选择其他,然后浏览到python.exe.确保选中始终使用此程序打开此类型的文件"框.在此处.
  1. Edit your Options line of httpd.conf to include ExecCGI
  2. Register your python file with httpd.conf as a cgi handler by adding the following line:
    AddHandler .py
  3. Restart Apache
  4. Be sure that your shebang line (the #!/usr/bin/env python bit on top) actually points at the path to your python executable. For python2.6 living on C:, this might read:
    #!\Python26\python
  5. If your scripts must be portable, instead of modifying the shebang line add the line ScriptInterpreterSource registry to your httpd.conf, and make sure that windows opens *.py files with Python.exe by default. This can be tested by double-clicking a script. If it doesn't execute, right-click it, choose open-with, then other, and then browse to your python.exe. Make sure to check the "always use this program to open files of this type" box. There's a Microsoft Knowledge Base article on this here.

最后,mod_wsgi或FastCGI是使Apache执行python的首选方法,并且优先选择流量较低的站点(每天十万个请求中的十个).我还建议您研究一下web框架,例如web.py(非常轻巧)或django(重量较重,但是如果您要收集用户输入或与数据库接口,则可以节省大量时间).

Finally, mod_wsgi or FastCGI is the preferred method for getting Apache to execute python, with the prior holding preference to lower traffic sites (10's of thousands of requests per day). I'd also advise looking into web frameworks such as web.py (very lightweight) or django (heavier weight, but saves you tons of time if you're collecting user input or interfacing with a database).

  • mod_wsgi setup instructions.
  • FastCGI documentation (getting it setup on windows can be a pain)
  • Django getting started
  • web.py tutorial

这篇关于.py文件在浏览器中显示代码,而不是运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆