Beginner Python question about making a web app [closed]
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this questionI am completely new to Python-- never used it before today. I am interested in devloping Python applications for the web. I would like to check to see if my web server supports WSGI or running python apps in some way.
Let's say I have a .py file that prints "Hello world!". How can I test to see if my server supports processing this file?
FYI, this is a Mac OS X server 10.5. So I know Python is installed (It's installed on Mac OS X by default), but I don't know if it's set up to process .py files server-side and return the results.
BTW, I'm coming from a PHP background, so this is a bit foreign to me. I've looked at the python docs re: wgsi, cgi, etc. but since I haven't done anything concrete yet, it's not quite making sense.
A very basic WSGI application can look as follows:
def application(environ, start_response):
start_response('200 OK', [('content-type', 'text/html')])
return ['Hello world!']
Unfortunately, if you put this into helloworld.py on the server and then browse to URL/helloworld.py you will most likely see the code.
In general you need to add very specific configuration options to the server (or to a server configuration file) to get it to serve your python 'application' correctly. Given you are using mod_wsgi on Apache 2, a configuration could look as follows:
<VirtualHost *>
ServerName example.com
WSGIScriptAlias /server/location/address /path/to/helloworld.py
</VirtualHost>
Where /server/location/address is the endpoint of the URL you have to browse to.
This is because python WSGI catches all URLs passed to it, and pushes them to the same entry point (your application method/class). And from the information received in the parameters, the application must decide what page to return.
Since this information is so application specific there 'should' be a way to configure it on the server, however I have yet to come across a web-hosting configuration panel that allows the configuration of Python applications. This generally means you have to contact the server administrators and have them configure it for you.
However, in general, when you sign up for hosting the company generally has a page where they tell you exactly what is supported on their servers (generally: php, mysql) and how much space and bandwidth you are allowed. Thus if they do not list it on their site, it is highly likely they will not support it.
To get around this you can instead buy a VPS (Virtual Private Server) and then configure it however you want.
If you are new to Python and Python web application development, then ignore all the hosting issues to begin with and don't start from scratch. Simply go get a full featured Python web framework such as Django or web2py and learn how to write Python web applications using their in built development web server. You will only cause yourself much pain by trying to solve distinct problem of production web hosting first.
精彩评论