Join 3,512 readers in helping fund MetaFilter (Hide)


Unleash the snake!
February 8, 2012 9:24 AM   Subscribe

How do I put my stupid simple Python scripts... on the Internet?

Working my way through Learn Python the Hard Way, and it's pretty cool, but I'd love to be able to learn by doing, more than by rote code: a challenge I'd like to set for myself is to try to create something useful out of every lesson, and toss it online for other people to see/use. This would also require me to bone up on my CSS and page design skills in tandem with the Python-learning.

I'm sure the book will get around to how to do this eventually (or not), but I'd like to start doing it now, and have a page of "stuff I made while learning Python," each linking to its own subpage of the, well, stuff I learned.

So I'm looking for lessons on how to take "helloworld.py" and turn it into "helloworld.html," where the Python script executes in a nice Web page environment.

Notes:
- I'm an egotistical SOB. I would like to pretty up and keep these pages as part of my personal site, not stick them in a public code bin.
- My host (Webfaction) is apparently excellent as a Python/Django host, so no worries there.
- My html and CSS skills are limited -- I can build a simple Web page, but I'm kind of a toadstool when it comes to things like sophisticated floats, javascript, PHP, etc.
posted by Shepherd to Computers & Internet (4 answers total) 6 users marked this as a favorite
 
Running python as CGI is fairly simple if your webhost supports it (which it sounds like it does). Think of it as:

1. stick your python file on your web server (say, in the web root) and mark it executable
2. web server gets a request for "http://example.com/helloworld.py" and realizes that the URL resolves to an executable script "helloworld.py" that it needs to run
3. web server makes a text buffer in memory, sticks the HTTP headers "200 OK" etc. there that will go to the browser, then executes your script
4. anything you write to standard out (i.e., "print") will get appended to the same text buffer as web page content
5. so all you need to do is something like print "<html><body><pre>" and then start printing the program output like normal. Then print "<pre></body></html>" when you're done. When your script exits, the web server will send everything along to the browser.

Of course, if you want the page to look fancier, you can, as part of your prefix printing, output an HTML header with CSS, etc. But once you see the base example working it'll click what you need to do.
posted by introp at 10:05 AM on February 8, 2012


Google App Engine
posted by empath at 10:10 AM on February 8, 2012 [1 favorite]


You can start with the Django documentation. Make an app that runs locally. It sounds like your web host will have some documentation on how to get your Django app running there.

I'm a big fan of Flask. You could try it as well.
posted by aloysius on the mixing boards at 10:11 PM on February 8, 2012


A long-delayed follow-up: I messed around with CGI for a while, but then abandoned that and moved straight to Django, dropping Python lessons in favour of trying to figure out Django and retroactively figuring out Python as required. It's laborious, but I think ultimately will be the most rewarding scenario.
posted by Shepherd at 6:08 AM on July 4, 2012


« Older Can anyone recommend an accoun...   |  I recently reconnected with a ... Newer »
This thread is closed to new comments.