Python

To run a CGI script at Loopia the following is required:

  1. The (sub)domain must be on one of our UNIX servers.
  2. The script must be located in the cgi-bin directory.
  3. The file extension must be .py for tor this to work.
  4. The file permissions must be 755 (CHMOD). You can set this with a FTP-client.
  5. Syntax should be for Python 2
  6. The path to Python is: /usr/local/bin/python2.7
  7. The path to Sendmail is: /usr/sbin/sendmail
  8. The file needs to be saved UNIX format so the right linefeed is used.
  9. The file public_html/.htaccess needs to include SetEnv HOME.

A code example:

#!/usr/local/bin/python2.7
# -*- coding: UTF-8 -*-
print "Content-type: text/html"
print
print "Hello, World!"

Our support do not cover script related problems that is not caused by one of our servers or similar, and we are unable to assist you with your programming.

If you have any problem with a script and want us to have a look at it, send the path to the script, a brief explanation of what you are trying to do and if you get an error messages to us.

Connect to database

You can also connect to the MySQL databases with Python. For this you use a module named MySQLdb. Sample code that connects to a database and retrieves version information for MySQL looks like this:

#!/usr/local/bin/python2.7
import MySQLdb
print "Content-type: text/html"
print

db = MySQLdb.connect("server", "user", "pass", "db")
db_cursor = db.cursor()

db_cursor.execute("SELECT version()")
row = db_cursor.fetchone()
print  row[0]
db_cursor.close()

db.close()
Was this article helpful?

Related Articles