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
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. Thenprint "<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