0

I have a very simple Python script that I want to run though my website. Here is my script:

#!/usr/bin/python
print("This line will be printed.")

Suppose my script is called 'hello.py' and my website is 'mywebsite.com'. My web hosting is provided by BlueHost, and I can access the server through FileZilla. I place 'hello.py' in the public_html directory on the server (which also contains my website html files). Now I try to run the Python script though the browser, so in my web browser I go to 'mywebsite.com/hello.py'. In the web browser, the source code of 'hello.py' is printed. Is there a way to execute the Python script instead?

3
  • 2
    my.bluehost.com/hosting/help/search?search=python Commented Sep 20, 2020 at 18:11
  • I do have Python installed on the server, and running the script through SSH works successfully. It's just not working via the browser. Commented Sep 20, 2020 at 18:21
  • Put it in the cgi-bin directory Commented Sep 20, 2020 at 18:22

1 Answer 1

1

The following modifications should be made for success:

(1) Python script slightly modified to the following:

#!/usr/bin/python
print("Content-Type: text/html")
print

print("This line will be printed.")

(2) Add the following into the 'IfModule mod_rewrite.c' field of your .htaccess file:

<IfModule mod_rewrite.c>
Options +ExecCGI
AddHandler cgi-script .py
</IfModule>

Here is the successful output result on my browser, after searching 'mywebsite.com/hello.py':

Successful execution

Contrast this to the output result if either instructions (1) or (2) are not followed - the browser simply outputs the Python source code. For instance, violating instruction (2) yields:

Failed execution

Note that the BlueHost Python or CGI guides do not provide this information. My solution was attained after a series of guess-and-checks, and there might be a more proper way of doing this.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.