How to Run a Python Script on a Server



Python


In this article, we show how to run a Python script on a server.

So we're going to run this Python script on a server hosted by godaddy.

Many web hosting companies install a Python interpreter on the server. This way, your website that you host from this company can run Python scripts.

In order to run a Python script, you just need to do a few tweaks in a few places.

So let's create a Python file. Let's call it test.py.

In the following code below, we show the contents of this test.py file.



So the first thing we do is specify the location of the Python interpreter on the web server.

Usually the Python interpreter is located in the bin directory.

The bin directory is the directory in linux that contains the ready-to-run programs (also known as the executables). It is here that most likely will contain the Python interpreter for a web server. This directory also contains most of the basic Unix commands such as ls and cop. Most of the programs in the /bin directory are in binary formta, having been created by a C compiler.

So this is a very important line of code. We must specify where the Python interpreter on the server is located, or else the Python code will not work.

After this, we just have to specify that the file is an HTML file.

This allows HTML tags to be executed, since the file is an HTML file. Without this, none of the HTML commands will be executed.

Next we simply print out the statement in h1 tags, This is the home page

Since we want to show that the website can actually run Python code, we run a for loop that iterates 5 times through the loop.

From this for loop, we print out each number of the line. We then print out anything.

Again, this is just to show that Python code can be run on a server.

One last thing, most web hosting companies, including godaddy, do not yet have support for Python 3 (I don't know why). So you have to use Python 2 syntax in order to run Python code. You can see that above we have Python 2 syntax in regard to the print statement. To print in Python 2, we don't use parentheses. But in Python 3, we do. So in the above code, we leave out the parentheses and simply put the quotation marks.

So after you have this Python file (.py file), then you can upload it to any directory on your website.

Once the file is uploaded to the website, then you need to give this file certain permissions.

This is not just a regular static file. Since this file is a Python file and contains Python code, we need to have execute permissions on this file. Remember this is not just a static HTML file. With a scripting language such as Python, it's dynamic. It needs to execute Python code.

So I used the filezilla FTP software in order to upload this Python file to the website.

Once it is uploaded, right click the file and click 'File Permissions...'

This will open up a new window. In this new window, change the Numeric value to 755.

The numeric value of 755 gives execute permissions to the file, so that Python can execute on it.

Once you have done this, you go go to the web browser and type in the path to the file.

You should now see the Python code which we wrote executed.

This is shown in the output below.


Python script running on a web server


And this is how to run Python code on a web server.


Related Resources

How to Randomly Select From or Shuffle a List in Python



HTML Comment Box is loading comments...