Jump to content

Simple Python Cgi Script


palaniza

Recommended Posts

 

 

Hi, friends!

My site is: apiapi.heliohost.org

My server, I use is: johnny.heliohost.org

 

I want to run a simple Python CGI script

Reading this topic (https://www.helionet.org/index/topic/29035-basic-python-script-500-internal-server-erorr/?hl=python), I decide open new one because it is not work in my case.

 

1) I create a file with simple code (tacked from link below). Using Notepad, save it as file with .cgi extension (actually I tried .py extension to) with UTF-8 encoding (actually I tried UTF-8 without BOM | ANSI encoding to).

Code I used:

#!/usr/bin/python3.6

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

print("Hello")

 

======= also try =======

#!/usr/bin/python3

print("Content-Type: text/html")

print

print("Hello")

 

======= also try =======

#!/usr/bin/python3

print "Content-Type: text/html"

print

print "Hello"

 

======= also try =======

#!/usr/bin/env python

print "Content-Type: text/html"

print

print """\

<html>

<body>

<h2>Hello World!</h2>

</body>

</html>

"""

 

2) Using cPanel --> File Manager I was upload my file into /public_html/cgi-bin/ directory

 

3) I create a .htaccess file with content

AddHandler python-program .py

and upload it into /public_html/cgi-bin/ directory (actually I attempt to run my script with/without it file)

 

4) Using cPanel --> Apache Handlers I was Create an Apache Handler. I Fill next fields respectively:

Handler: python-program

Extension(s): .py

 

5) Using my browser (chrome | IE) I was try to run my script by link: apiapi.heliohost.org/cgi-bin/hello.cgi

500 server error was occupied

If my file extension is .py - just source code is shown as result

 

5) Also I try to fetch my script recuvest thru html-page:

<html>

<head>

<title>script</title>

</head>

<body bgcolor="black" text="#FF9933" link="blue" vlink="purple" alink="red">

<form action="http://apiapi.heliohost.org/cgi-bin/hello.cgi">Enter:

<input type=submit value=Go>

</form>

</body>

</html>

 

Result always is the same

 

I do not know what to do...

Dear moderators, can You, please add some Wiki-page with step-by-step instruction with content describes simple Python CGI script performing.

 

Tell my please, were can I find error log file to debug my CGI scripts

 

Tank You so-so much for Your suggestions

Link to comment
Share on other sites

Delete everthing thing (.htaccess, apache handler,....)

and try use this:

 

#!/usr/bin/python

print("Content-Type: text/html")

print("")

print("Hello")

 

or this if the above doesn't work, but the above worked for my script. :

 

#!/usr/bin/python

print("Content-Type: text/html")

print()

print("Hello")

 

Remember to chmod the cgi-bin folder and the file to 755, not 644 nor 777, must be 755 to work.

If you're using Python 2.x then remove the "(" and ")"

Link to comment
Share on other sites

Um. No it's not:

root@johnny [/home/palyuri/public_html/cgi-bin]# ls -l
total 4
-rw-r--r--. 1 palyuri palyuri 72 Aug 25 06:34 hello.cgi
If it was 755 it would say -rwxr-xr-x. Right now it's 644. What 644 means is owner can read and write, group can read, and other can read. What 755 means is owner can read, write and execute, group can read and execute, and other can read and execute. You can change the permission through SFTP or with the file manager in cpanel https://johnny.heliohost.org:2083/frontend/paper_lantern/filemanager/index.html
Link to comment
Share on other sites

  • 4 weeks later...

The next problem is you're using dos line endings on a linux system. Cgi is very particular about line endings. The easiest way to make sure you're encoding your file correctly and using the proper line endings is to use the cpanel filemanager, and then click edit code. Either that or you can use a windows editor that knows how to output linux line endings like notepad++.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...