May 26

Updating Python of 1and1 shared hosting

Since recently I am learning Python. It was something in my ToDO list from some time ago, so I installed the newest version of Python (at that moment 3.3.2) in my computer and I started learning. After some time, I knew the basics and I decided to look for a hosting that will let me use Python in their servers. Then a surprise come to me: 1and1, the hosting where I have my blog, has Python as one of their programming languages. So I put my hands to work and make the “Hello World” in 1and1 servers. Remember that all the files that you want to execute via a browser needs to have the permission set to 755, if not you will see a 500 error at your browser.

Hello world in Python 1and1 shared hosting

A basic “Hello World” in 1and1 servers will be a “helloworld.py” with the next content:

#!/usr/bin/python
print "Content-Type: text/html \n\n"
print "Hello World"

The first line has the shebang with the path to the Python executable. Because the file is going to be referenced via a browser, I need to send the headers of the HTML with a few blank lines at the end. If you don’t do that you will have a 500 Error trying to access via a browser. The last line will print the “Hello World”.

Check version of Python of 1and1 shared hosting

After the success of “Hello World” I check which Python version is installed at 1and1 servers. You could do it with a “getVersion.py” file, and access again via a browser.

#!/usr/bin/python

import sys

print "Content-Type: text/html \n\n"
print sys.version

The output of the browser was this:

2.6.6 (r266:84292, Dec 27 2010, 00:02:40) [GCC 4.4.5]

That was not good news for me. Python 2.6.6 is an old version of Python, and I started learning the newest one. Is there any way to solve this problem?

Install new python in 1and1 shared hosting

Yes, there is a way to solve this. Since 1and1 allows to access their servers through SSH is possible to install our “custom” Python. If you don’t know what is or how to use ssh, you can take a look to 1and1 SSH FAQ. The steps down here sould be made in order to install Python-3.3.2 in a folder called custom at the root path of your webhosting.

mkdir -p ~/src
mkdir -p ~/custom
cd ~/src
wget http://www.python.org/ftp/python/3.3.2/Python-3.3.2.tgz
tar xzf Python-3.3.2.tgz
rm Python-3.3.2.tgz
mv Python-3.3.2 python-3.3.2
cd ~/src/python-3.3.2
./configure
make altinstall prefix=~/custom exec-prefix=~/custom
cd ~/custom/bin
ln -s python3.3 python

After this steps you will have the new version 3.3.2 installed in your shared hosting. If you want to use it from the SSH terminal, it would be a good idea to add the path of the custom python to the PATH, so when you call python it will execute the new one:

echo 'export PATH=$HOME/custom/bin:$PATH' >> ~/.bash_profile
source ~/.bash_profile

Now you could call python and see that the new version is running.

Hello world with the new custom Python

And how this affect to my python scripts referenced via a browser? Well, using the custom python was easier as I thought. Do you remember the first line with the #!? This time, instead of writting the path to the python installed by 1and1, write the complete path to the custom python. If you don’t know the complete path, you can use the SSH again to go inside the custom/bin folder created before and call pwd to see it.

#!/kunden/homepages/XX/dXXXXXXXXX/htdocs/custom/bin/python

import sys

print("Content-Type: text/html \n\n")
print(sys.version)

And now, like magic, the version used by the script is the new one 3.3.2. Be careful with this new hello World, since now we are running a newer version of Python and, for example, the print sentence is different from the previous one. The result:

3.3.2 (default, May 25 2013, 13:31:52) [GCC 4.4.5]

Apr 30

My English Blog

Welcome visitor!

If you see this so empty is because I have just begin working on this English Blog. I will write here about different projects related to programming stuff I have made (and of course projects I will make).

If you want to know something about me, just jump to the page “Hello World!” and feel free to ask anything in the comments.

Come back to see any update!