Posts Tagged ‘python programming language’

By Mike Hubbartt, © Copyright 2011, All Rights Reserved.

Python is a good programming language for web developers, and I enjoyed my first experience with it. Our web development class was given a simple python web server and some HTML files and told to extend both to render images. Since I had not used it, I first spent time tracking down python information: where to get it, how to install it, and how to develop using it.

There were online sources for it, but I was pleased to discover that Apple ships python on Macs. My 1 yr old 2.26 GHz dual core Macbook has Python 2.6.1, so no download was necessary. I found some information on the internet with the command line syntax for starting python so I decided to use a Terminal to launch python, edit the python server source files and HTML files with TextWrangler, and view the HTML with FireFox.

Getting Started With Python

This is the process I followed:

Python running on port 9000

1. Put the python server code and HTML files in a folder on the computer. I created a python folder in my user directory (eg. /mikehubbartt/python/).

2. Start the Mac OSX Terminal.

3. Use cd <dir> to change to the directory with the python server code and HTML files.

4. At the Terminal, enter ‘python <> and press the Return/Enter key. The python server is now running, using the port specified by ‘PORT_NUMBER = ‘ in the python server source code.

5. Use the browser to access the main HTML file (index.html) via the python web server, using this syntax:


NOTE: You append the port number to localhost in the URL, which is specified in the file to be able to access the content via a web browser. If you specify a different port number, make sure you use that port when accessing content.

6. Now you see the first file with your web browser, and the Terminal content will now change as you select links on the web pages (see my second screen shot at the right for an example – I’ve clicked good and bad links to show the error messages seen at the Terminal).

The next steps to modify the server source code and HTML source files (I used TextWrangler, but any other program is fine – it is preference of the developer) are project specific, so I won’t spoil it for you by giving the exact steps, although I will give some tips.

Python Tips

Tip 1. Any changes to the python server code require modifying the source code, stopping the server, then restarting the server. The way to stop the python server is to select the terminal window and then press control-c. Making any desired changes to the python source code and save the changes, then switch back to the Terminal and press the up arrow key on the keyboard to retrieve the previous Terminal command and then press the Return/Enter key to restart the python server.

Tip 2. Any changes to the HTML files is seen after changing and saving those files, then refreshing the browser, unless the python server code is causing the problem.

Tip 3. When given any partial source code, look at the existing code and see if there are existing elements that could be used in conjunction with a programming language reference to provide similar functionality.

Tip 4. Remember that you can send images and text as well as HTML pages to a web browser, without needing to embed the text and image in an HTML file with a python web server.

Tip 5. When links are clicked in the HTML files, that header information is reflected in the terminal window, so it is helpful to have the editor, browser, and terminal window accessible at the same screen. Use the Terminal information to help track down and debug your code.

Tip 6. Do NOT run the python server in more than 1 Terminal at a time.

Tip 7. Always terminate the python server with control-c before closing the Terminal window.

Tip 8. Make sure you have images in some of the HTML files, so you can see how that header information differs from HTML files just containing text.

Tip 9. Locate and print a handy python quick reference guide. Also locate and bookmark good URLs like the official python programming language site.

Tip 10. Play with it. It takes remarkably few lines of source code to create a python web server, and it is a fun language to use.