Python noob here: On a Python enabled web server, how do I use Python?

时光毁灭记忆、已成空白 提交于 2019-12-30 07:46:08

问题


#!/usr/bin/env python
# -*- coding: UTF-8 -*-

# enable debugging
import cgitb
cgitb.enable()

print "Content-Type: text/plain;charset=utf-8"
print

print "Hello World!"

My goal is to replace PHP with Python. I'm pretty good with PHP and I can use Python on my own local machine, but I can't get it to work on my web server. My web host says they support Python, so I must be doing something wrong.

Now, Python is associated with CGI. Do python files have to go into my cgi-bin folder? I've never seen a web file with a py extension or a cgi extension, I don't know how these things work, I'm really only familiar with PHP.

I watched the first hour of Google's "Learn Python" class and that only talks about running Python locally.

:( Sorry I'm so nub, please fix me.


回答1:


That's a perfectly acceptable CGI script. If you are using Apache, it will need to go into your cgi-bin, need to be executable (I may be wrong on this), and you should name it with the common extension for the language .py, or with .cgi.

If you can control what files your webserver will consider CGI, you can place the file wherever you want. Chances are, you can't control that. You can view more information on CGI in Apache here: http://httpd.apache.org/docs/2.2/howto/cgi.html

The basic gist is that Apache will not treat a file as a CGI file unless two conditions are met: The cgi-script will need to be activated using the AddHandler or SetHandler directives, and ExecCGI will need to be enabled in the Options directive. Typically, this is not available to the user in a shared hosting environment.

EDIT: To clarify, CGI is just a mechanism for you to write scripts in any arbitrary language, and have your webserver execute them and send the output from that script to your clients. It's not a recommended approach for anything but a simple script, due to the fact that on every single request the server will fire up a new instance of your interpreter. For Python, the best solution would be a WSGI compatible framework, such as Flask, Bottle, or Django. The first two are micro-frameworks that try to stay out of your way, while Django is a full-stack framework that provides a whole lot of glue.




回答2:


The Python way to do web applications has more in common with something like CodeIgniter or Cake PHP.

Start with Django, just try the tutorial. The most common complaint about Django from die-hard PHP developers is that Django will not let you mix code and HTML - and it IS a good thing.

Of course you can do plain old CGI, but it is the hard way.




回答3:


Depending on your web hosting company, the #!/usr/bin/env python line may need to read a bit differently. Some include a version number on "python" and some have python in a different directory. You'll need to check with your provider.

Also, you need to give the world execute (NOT write) permission on the file, since you are WANTING people to execute the script.

What happens now if you upload that code with a .cgi or .py extension in your cgi-bin folder and then go to it in your browser?

Also, if you google "python cgi" you'll find a lot of tutorials. For simple stuff without tons of hits, cgi will work fine, although as others have said, there are other mechanisms that are far more resource efficient when that's needed.

You can't mix python and html on the same page, but there are mechanisms (see python and server side include or SSI) for writing an html page with a placemark that calls your python program and puts the response from the python program there in the resulting displayed html.



来源:https://stackoverflow.com/questions/4587416/python-noob-here-on-a-python-enabled-web-server-how-do-i-use-python

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!