Python Setup

Status
Not open for further replies.

nilamo

New Member
Messages
2
Reaction score
0
Points
0
I'm currently learning python, and since cgi is a major part I thought I'd try to find an account where I could test a few things out and make sure they work. The following program was copied pretty much line by line from a book, and should show the current date and time in the browser window. To this end, I saved the file in the cgi-bin directory, changed the permissions to 755, and pointed my browser to http://nilamo.x10hosting.com/cgi-bin/time.py, and saw a 500 Internal Server Error. Is there a step I forgot? Any help would be greatly appreciated.

Code:
#!/usr/bin/python
import time

def printHeader(title):
    print """Content-type: text/html
    
    <?xml version = "1.0" encoding = "UTF-8"?>
    <!DOCTYPE html PUBLIC
        "-//W3C//DTD XHTML 1.0 Strict//EN"
        "DTD/xhtml1-strict.dtd">
    <html xmlns = "http://www.w3.org/1999/xhtml">
    <head><title>%s</title></head>

<body>""" % title

printHeader("Current date and time")
print time.ctime(time.time() )
print "</body></html>"
 

Executron

New Member
Messages
6
Reaction score
0
Points
0
Not 100% sure, but:
The .py files are not "executables". The server needs to have the python interpreter installed, to "translate" the commands in your .py file.
As far as I know, from:
http://x10hosting.com/hosting
python isn't supported atm.
 

Slothie

New Member
Messages
1,429
Reaction score
0
Points
0
They probably wouldn't install python either, its too easily abusable resource wise.
 

Bryon

I Fix Things
Messages
8,149
Reaction score
101
Points
48
Python is installed and it working correctly.

The script that you linked to is giving the following error:
Code:
[Sat Oct 27 02:20:55 2007] [error] [client 129.21.113.23] malformed header from script. Bad header=\t: /home/nilamo/public_html/cgi-bin/time.py
Traceback (most recent call last):
  File "time.py", line 2, in ?
    import time
  File "/home/nilamo/public_html/cgi-bin/time.py", line 17, in ?
    print time.ctime(time.time() )
AttributeError: 'module' object has no attribute 'ctime'
 
Last edited:

Dannz

New Member
Messages
43
Reaction score
0
Points
0
Hi.

A few comments and suggestions for starting out as a complete novice...

Python is supported.

You can run Python scripts from any directory - it doesn't have to be in the cgi-bin directory.

I usually use the .py extension with my Python scripts. If you do this you need to set the Apache handler to recognise py extension as cgi-script. You can do this from cPanel (the Apache handlers section at the bottom). Without this handler setting the script won't be recognised.

Have you got a Python IDE like Pyscripter? It helps to have your own Python dev environment for testing.

What works in a Python interpreter does not always work on a server. The script you have is fine in an IDE, but the problem is a bit of formatting and how things are handled.

Begin simple, use exception trapping...

Set permissions to 711

Pay attention to what you call your script - if its called 'time.py' then its going to cause a problem when you import time (i.e. this will import time.py which means you could be attempting to import your own script!)

Try this to begin with -

#!/usr/bin/python

print "Content-type: text/html\n\n"

try:
....import time
except:
....print "could not import time"

print time.ctime()



BTW replace the .... in the try/except with spaces - but spacing doesn't save in the posts, so I put in the dots instead.

What do you want to do with Python in cgi? I can't help much on formatting and headers since I use XMLRPC which is called from Flash swfs (using Python as middleware to call MySQL database and call various functions written in Python). I don't do HTML, so can't really give many pointers there, but if you use Flash, then there is lots of nice ways of having the two work together.

Suggest you then try a few functions in python, then perhaps see how you get on with trying to link to a database (the MySQLdb module is installed on the server). Try a search on Python in the forums - there are a few things there.
 
Last edited:
Status
Not open for further replies.
Top