开发者

Web Python Question

Can anyone assist me in getting a Python script running on Hostgator Shared hosting? I work with PHP mostly, but have taken a liking to Python, and would like to try to get it going on the web. The only w开发者_JAVA百科ay I've ever ran Python is with either the interpreter, or through a terminal, with >Python file.py. I tried just uploading a hello world file to the webserver, but all it outputs is the script source. I talked with hostgator, but all they could tell me was I need to use a dispatcher, which I cannot seem to find an example of. All I want to know, is how to make a <p>Hello</p> output to the browser.

Thanks, and sorry if I'm rambley, I've been Googling this off and on all week now.


Well, I got this from Hostgator's own support site.

Assuming your host is running Python 2.x, then you can adapt the linked-to script as follows:

#!/usr/bin/python
print "Content-type: text/html\r\n\r\n"
print "<html><head>"
print "<title>CGI Test</title>"
print "</head><body>"
print "<p>Test page using Python</p>"
print "</body></html>"

and put it in your cgi-bin folder with permissions of 755.

Update: In terms of "getting around" the cgi-bin folder: that'll depend on options your hosting package allows. Look at Bottle for a simple dispatcher which fits in a single Python module. You can deploy it using CGI,

import bottle
# Put your bottle code here, following the docs on the Bottle site
bottle.run(server=bottle.CGIServer)

Further update: Apart from the Bottle docs, I suggest you read the Python docs about CGI.


Briefly you need modify your .htaccess file to allow Apache to interpret python file as cgi script, then write your python script with "#!/usr/bin/python" and don't forget to output http header (otherwise you will get a 500 internal error)

You can check this post to get the complete instruction


I don't know about Hostgator, but you want your web server to see your Python script as an executable CGI script. That means making it executable and ensuring there's a #! in it and either putting it in cgi-bin, or setting your server to recognize .py as a CGI script file extension. Here is a simple example script and some context using the cgi module.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜