How to run scripts in Python?

Modified on Thu, 18 Jan at 11:27 AM

All files specified in the examples should be located in the /domains/YOURDOMAIN/public_html directory.

Minimum code of the running script.


Don't forget to set permissions on the script to 755 (chmod +x run.py)


File: run.py

#!/usr/bin/env python
# -*- coding: UTF-8 -*-

import wsgiref.handlers

def application(environ, start_response):
    start_response('200 OK', [('Content-Type''text/plain')])
    return ['Hello World!\n']

wsgiref.handlers.CGIHandler().run(application)


Next, create a .htaccess file with the following contents


File: .htaccess

Options -Indexes +ExecCGI
AddHandler cgi-script .py
DirectoryIndex run.py

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !/run.py
RewriteRule ^(.*)$ /run.py/$1 [L]



If the script is not located in the root directory of the site, in a subdirectory, then in .htaccess it is necessary to specify the full path to the script. For example:


DirectoryIndex /home/NAMEUSER/domains/YOURDOMAIN/public_html/app/index.py

Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select at least one of the reasons
CAPTCHA verification is required.

Feedback sent

We appreciate your effort and will try to fix the article