0

I 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.

Jeff
  • 12,147
  • 10
  • 51
  • 87
  • 1
    What "server" are you talking about? A web server is a pretty specific piece of software. For example, the Apache httpd server. What "server" software are you asking about? – S.Lott Nov 16 '10 at 22:00
  • As I mentioned, I'm using Mac OS X server 10.5. This uses Apache by default (I believe). – Jeff Nov 16 '10 at 22:02
  • Mac OS X can use Apache. That's not the point. Are **you** thinking of using Apache? You have to pick a server. What server are you going to use? – S.Lott Nov 16 '10 at 22:09
  • @S.Lott: I'm not setting up my own server, and I'm not shopping for hosting either. I have webspace, and I want to see if it's capable of executing my python script. I don't have admin rights, so if it doesn't then it doesn't... I just want to know. – Jeff Nov 16 '10 at 22:11
  • Mac OS X server is a version of Mac OS X sold by Apple, with Apache pre-installed and pre-configured. I should clarify that I meant the server I'm using is Mac OS X server. My computer is Mac OS X as well, but that's completely irrelevant. – Jeff Nov 16 '10 at 22:13

2 Answers2

5

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.

Mike
  • 1,060
  • 2
  • 11
  • 14
  • Thanks, this is helpful! I have a few follow-up questions if you don't mind? There is a shared cgi-bin on the server, and I noticed there is a test.py file in there that works: http://chil.rice.edu/cgi-bin/test.py My python file gives an error message. I remember reading that any changes to a python file require the web server to restart (when using CGI method), so presumably if I restart the server it should work correctly, right? As far as WSGI goes, I guess it's safe to assume I can't use WSGI without contacting my sysadmin, right? – Jeff Nov 16 '10 at 22:56
  • It depends on how they have configured the python system. The server header returned is: Server - Apache/2.2.14 (Unix) mod_ssl/2.2.14 OpenSSL/0.9.7l DAV/2 PHP/5.2.12. So I assume they are using mod_python, in which case yes you will need to somehow restart Apache to get it to serve a newer version of the file. There are ways to get around this (using automatic code reloading) if they are using mod_wsgi, see http://code.google.com/p/modwsgi/wiki/ReloadingSourceCode. – Mike Nov 16 '10 at 23:14
3

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.

Graham Dumpleton
  • 57,726
  • 6
  • 119
  • 134