Stonerocket logo free web hosting
Back to Webmaster Topics - Home

Python Information and Tutorials.


If you plan to use python on your web hosting account then we recommend that you read this great article written by stonerocket member Snowman.
Added: 29th Dec 2007

What does this page contain?:

::What is and Why Python?
::The logo
::Who uses Python and for what to use Python?
::Future of Python?
::Where to learn Python?
::All great, How to get it working on my host?
::Looking for that special function...
::Editors
::Extra stuff...
::FAQ
::WAMP for testing
::The php.info() for Python

What is and Why Python?

Personally I find it an easy, fun and good to learn language that can be used for normal programming as well for generating web pages. A long time ago I tried to learn C, but that was no fun.
The programming code is very clear to make and read. Instead of using brackets you will use tabs and enters, i.e. space.
Something I like a lot is the quote "batteries included", this means that the standard library is enormous.
And don't forget it's open: freely usable and distributable, even for commercial use!
Python.org has it's own way to describe Python:

More info: http://www.python.org/about/

I found a post by a moderator called vegaseat on daniweb.com and I find his post interesting to quote.

The logo


The powered by logo:


You can download the logo's in different file types. I like the .svg a lot (used in Inkscape). See for download and more info: http://www.python.org/community/logos/
If your using them, please read the guidelines first: http://www.python.org/psf/trademarks/
Or contact python.org about it: psf@python.org

Where can I find Python?

At the website of course: www.python.org , but for on your web host you don't need to download anything, because Stonerocket has it already installed!
Like to try Python out and have a good IDLE: http://www.python.org/download/
The download contains (version 2.5):
The Python Shell looks like this:
The 'extra' file option:
The IDLE look:

Who uses Python and for what to use Python?

Python is being used by big names. The most know ones: Google, NASA, Rackspace and Industrial Light and Magic.
Because Python has so much standard library's it can do the following:

Operating Systems?

Don't worry, Python can run on a lot: Windows, Linux/Unix, OS/2, Mac, Amiga, among others. There are even versions that runs on .NET, the Java virtual machine, and Nokia Series 60 cell phones.

Future of Python?

Currently the people that made Python are making a version 3, which will be even better than the current one, but also incompatible with older versions. No worries, it will take some time before it's done and I guess you don't need to relearn the language, besides that, most of the standard library's will be kept in it and deprecated features removed. You can try the alpha version here (with info): http://www.python.org/download/releases/3.0/
Personally I see a bright future for Python. The creator (Guido van Rossum) has been hired by Google, which looks to me like a good step. In an article he says he even gets 50% of it's time to work on Python outside Google.
Guido van Rossum is a Dutch guy. : http://en.wikipedia.org/wiki/Guido_van_Rossum
From Wikipedia:

Where to learn Python?

A good website to learn Python (and learning to program) is this website: http://www.freenetpages.co.uk/hp/alan.gauld/ It also covers Javascript and VBScript at the same time to show the difference in languages.

All great, How to get it working on my host?

Just like Perl you need to put the files in the cgi-bin and don't forget to set the permissions!
For stonerocket you must put the file (script) in: /public_html/cgi-bin/

example:


I use permissions "755" (with FileZilla Client). Which looks like this:

You can use the extensions: .cgi and .py
In the file you can add for different operating systems (linux, i think) this

or

The # is for a comment. everything after that is not read/used by the computer. But in the """ example text """ you cannot use the #.
The word print does what it says, print that text/var/calculation.
After that, add the print statement with what for file type it is, so the browser can read it good. I like to use html, see example:

None of the code besides what comes after print comes in the web code.
Notice the second print with no text. That is to create a blank line in the web code (this is required to work correctly).

It's a good thing to start now already with adding comments, so when you review your code again (maybe 1 year later, you never know) you still know what and why you did it.

Example:

You are of course free to add/change comments the way you like, but keep it informative and not too short or too long. Also don't use comments that say what the code already says, that's not necessary. Use comments that explain why the code does that!

Now you can already create web pages with Python on your web host, but to get more out of it I suggest you take a look at following links and the next title.

More info (order of links is random):
http://www.cs.virginia.edu/~lab2q/
http://www.devshed.com/index2.php?option=content
http://www.upriss.org.uk/python/PythonCourse.html
http://www.python.org/doc/essays/ppt/sd99east/index.htm
http://wiki.python.org/moin/CgiScripts
http://webpython.codepoint.net/
http://www.penzilla.net/tutorials/python/cgi/index.shtml

* Try to visit several websites with tutorials, don't keep it at one, there are plenty out there and this will give you a wider perspective.

Looking for that special function...

You can find all the functions on the 'Python Library Reference' web page: http://docs.python.org/lib/lib.html

More specific to web hosting are these pages in the Library Reference web page:

http://docs.python.org/lib/markup.html
http://docs.python.org/lib/internet.html
Or are you looking for that special module? Take a look at this page: http://docs.python.org/lib/modindex.html

Editors

They say that editors are the extension of your hand and your mind when it comes to programming. So I did some searching and came up with the following editors. Of course you can also use the editor (IDLE) that comes with Python download, but your free to try others so I suggest you do.

There is a big list on Python.org http://wiki.python.org/moin/PythonEditors, but I will also post the ones I encounter often on the web.

PyScripter
website: http://mmm-experts.com/
Screenshot (after updated download)
You can change skins! (this is Office 2003 skin)
DrPython
website: http://drpython.sourceforge.net/ SPE IDE - Stani's Python Editor
website: http://pythonide.blogspot.com/

Extra stuff...

Blender
Blender is a free 3D modeling program that makes use of Python and this creates some nice options:
- add 'existing' scripts: http://www.blender.org/download/python-scripts/
- find more 'existing' scripts: http://wiki.blender.org/index.php/Scripts/Catalog
- create your own! : http://jmsoler.free.fr/didacticiel/blender/tutor/python_script00_en.htm

The Gimp
The Gimp is an image manipulating program for free. It's not as good as Photoshop but comes very close.
There is this Gimp-Python package (I have not yet tested it) that lets you 'control' The Gimp with Python written scripts.

More info:
http://www.jamesh.id.au/software/pygimp/
http://www.gimp.org/docs/python/index.html

Inkscape
Another free program but this one works with vectors. Very nice to work with. (don't you know what vectors are? info: http://en.wikipedia.org/wiki/Vector_graphics
Since version 0.42 it also supports running extensions, like Python scripts! With this you can do almost anything to your SVG documents.

source: http://www.inkscape.org/screenshots/index.php?lang=en&version=0.42

OpenOffice
The great free alternative for Microsoft Office is called OpenOffice and it (now) supports Python scripting. I think it will be useful to automate OpenOffice from Python.


More info:
- http://wiki.services.openoffice.org/wiki/Python
- http://udk.openoffice.org/python/python-bridge.html

PHP and Python
If you like the way PHP works in HTML, I'm sure your going to like Spyce (not spice)
Website: http://spyce.sourceforge.net/

FAQ

The official FAQ can be found here: http://www.python.org/doc/faq/ but I'm making also my own, with a main flavor about web hosting.

Q = Question
A = Answer

====== GENERAL ======

Q Where can I download Python?
A At http://www.python.org/download/

Q Where can I learn Python?
A The homepage of Python is very useful so I suggest you check that out first. If your new to programming, try this website: http://www.freenetpages.co.uk/hp/alan.gauld/ And of course, Google is your friend.

Q What to download from Python.org?
A There are indeed a lot of files on the download page. I suggest you take the latest version (currently 2.5.1) which is on the top of the page and download the file that fits your operating system. So, if your using windows, pick the file called 'Python x.x.x Windows Installer'. There are some different Windows download versions depending on your processor, but it's self explaining.

Q Where can I find new functions?
A Very simple, at the official library website: http://docs.python.org/lib/lib.html

Q Can I only create scripts with Python?
A No, there is a project called Py2Exe and it combines all the needed files into one executable. Info: http://www.py2exe.org/
Info about 'automating' executables: http://www.devx.com/opensource/Article/20247

Q Can I hide my source code with Python?
A Yes you can according to this post:



====== Web hosting ======

Q What for side-scripting is Python?
A It is server-side scripting like PHP and Perl. So it's not like Javascript, which is client-side.

Q Where to put my file/scripts?
A In the cgi-bin folder of your website. On stonerocket.net this is (standard): /public_html/cgi-bin/

Q I get a 500 error, what to do?
A First thing you should check are the permissions, they need to be 755 ( -rwxr-xr-r )

Q What are permissions?
A The access rights of a file. Because your python scripts can only work when they are executed you need to allow this. HTML, for example, does not need these permissions because your browser only reads the file to make it work.

Q What permissions do I need to set?
A 755 ( -rwxr-xr-r )

Q Where do I set the permissions?
A There are many ways to Rome, but I use Filezilla and press right mouse button on the file (that is already on the web host) and 'set permissions'. I'm sure your own preferred file-up-loader has this setting somewhere (search its FAQ, Google or the manual)

Q I only get to see a copy of the file/script...
A Are you sure you put the file in the cgi-bin?

Q What extension to use for my file/script?
A I noticed that .py and .cgi both work. I have not yet discovered any speed, compatibly or render issues between these two, so your free to pick the own you like. I think .cgi is more common for cgi files.

Q My script is not working...
A There can be many reasons. Are you sure you added the second print in your script? I read that it else might not work.

WAMP for testing

WAMP stands for Windows, Apache, MySQL and PHP. A Microsoft Windows-based variant of a LAMP. It gives you a webhost on your own PC and is there for great for testing your scripts! But, there is a small problem - Python isn't installed on it. Maybe I will host a version someday that is the same as SR so you can test, but for now just follow the next steps to make it work. You will also learn something about WAMP and it's great functionality.

1.

First we need WAMP, download it, 20mb, here (I used 2.0): http://www.wampserver.com/en/download.php

2.

Install it at the normal place so "c:\wamp" for easy use. When the installer asks about SMTP and Email, just leave it as it is, because the testing server won't go live (on internet) and don't start WAMP yet!

3.

Next step is to make WAMP use the right Apache version that SR also uses (1.3.39), so let's download (4mb) that from here: http://sourceforge.net/project/download/WampServer2-APACHE1339.exe

4.

Install it and don't start WAMP yet. If you did so, right mouse button and click exit. (you can start it again under start>programs>wampserver)

5.

Next step is the correct Python version as SR (2.3.4, but that one is not safe so we go to 2.3.5 for now), download (9mb) it here: http://www.python.org/ftp/python/2.3.5/Python-2.3.5.exe

6.

Install Python in the normal C disk for easy use. The www folder in "c:\wamp" is your folder that runs/open/used. It's THE folder! So in here are your (index.)html files and in the cgi-bin your Python scripts that can end on both .cgi and .py as we change that in a sec.

7.

Al right, now we need to change some settings in WAMP. Go to C:\wamp\bin\apache1.3.39\conf\ and open the file httpd.config with notepad (or notepad++) Don't use Words. In here scroll down or search to "#AddHandler cgi-script .cgi" and remove the # to active this 'thing'. Also copy the sentence and paste it under it and replace the .cgi with .py. So you get:



Also change "ScriptAlias /cgi-bin/ "cgi-bin/"" into:

8.

Now fire up WAMP from Start>Programs>wampserver and go to the little icon on the task bar. Click with the mouse on it and make your way to apache>version> and select 1.3.39.

9.

Last thing we need to do is give the python scripts the correct shebang line (#!). If you have Python installed normal (C disk) you can use:

10.

To start the script, type in your browser: http://127.0.0.1/cgi-bin/YOURSCRIPTNAME.py or http://127.0.0.1/cgi-bin/YOURSCRIPTNAME.cgi ! If you get a error, make sure that after changing the httpd.config file you restart the wamp services. ! Now don't forget to change the shebang lines when you upload the scripts to the real SR server! :)

The php.info() for Python

For php there is this command to get all the info of the server and php version, it's called php.info(). Python does not really have this, but someone made a good alternative for this. It's called the snakecharmer and you can find it from snakefarm: http://snakefarm.org/

Download link: http://snakefarm.org/snakecharmer.tar.gz



Back to Script`s - Home