How to use Python for web applications

Python is a very powerful language that can be easily used in various platforms. To give you an example, if you wanted to develop simple 2D games, working out with C/C++ and openGL would be an overkill. Especially if are just a hobby gamer who wants to create one for yourself too then try out python. Python as a language comes with so many additional frameworks that really makes things easy.

This article will work as a starter to help you begin your wonderful journey of web programming with python. The article will answer this exact question for starters: “How to use python with Godaddy (shared hosting) and Dreamhost. “.

python_internet_programming

Godaddy and Dreamhost make it particularly easy to host python pages on their shared hosting as well. All you have to do is the following.

Tutorial : python for web

The tutorial deals with creating a python script and using it in a webpage and to see the results on a browser.

1 – Create a python file:
Python files are python scripts written in a file with the extension “py”. To create a python script to upload into your server, create the file named python.pywhich is a test file that includes the code below.

[python]
#!/usr/bin/env python
print “Content-Type: text/plain\n\n”
print “Hey this is a test python page”
[/python]

This file prints the sentence “Hey this is a test python page” on the webpage when you open the page using a browser (e.g http://domain.com/folder/test.py). But wait !! follow the following steps before you try this out.

2 – Upload using FTP :
Python.py was create on your computer locally, most likely a folder on your desktop ;) . You now have to upload it to a server. It must be noted that python files cannot run on double clicking on your local computer since they have to go through a python interpreter. If you are developing desktop python applications then you have to invoke your python script as python test.py from the python prompt, mayb from IDLE ( a python development window).

To work with web applications of python, you will have to move your python file to a server. Also the server must have a python interpreter installed. Godaddy and Dreamhost provide a python interpreter by default, so you don’t have to worry about any setup at all.

You can now upload the file onto your Godaddy or dreamhost server. Here is a tutorial to use FTP. Once your file is uploaded you will be able to access your first web python script at http://domain.com/folder/test.py .

3 – Changing file permissions :
In order to allow python script to execute, you must provide ‘execute’ permissions to the file by changing CHMOD. Set the CHMOD of the test.py file to 755. If you are unsure about how this can be done, have a look at Find this tutorial on changing permissions.

4 – Test your python page
You now are perfectly set to view your page through the URL.

Here is a sample Python page hosted on a Godaddy hosted server.

Error ?:

Most often when you are new to python, you will encounter countless errors mainly due to the syntax. Python is case sensitive and also tabs/indentation are important unlike other languages. If you encounter a screen as below be sure to rewrite your test.py file. you can copy the code above as is to the clipboard and use it

error_python_page

No related posts.

Related posts brought to you by Yet Another Related Posts Plugin.

Category: Programming Comment »


Leave a Reply



Back to top