Python

bryanda

New Member
Messages
5
Reaction score
0
Points
0
Hi everybody,

I have problems with python. I would like to seek for help and advice.

Here is my code below:

#!/usr/bin/python
#!/usr/bin/env python

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

def hi():
print "hello"


I would like to call the function hi() using
http://sllib.x10hosting.com/sl_py/hello2.py/hi,

but the word "hello" didn't appear.
and i try another code:

#!/usr/bin/python
#!/usr/bin/env python

from mod_python import apache

def display(req):
req.log_error('handler')
req.content_type = 'text/html'
req.send_http_header()
req.write('')
req.write('Hello World!')
req.write('')
return apache.OK

but there is error and i think is the "mod_python" not found. How should i install the mod_python and if needed how should i create a support ticket?

I am now totally lost. Need help badly..
Anybody please help me. Deeply appreciate it.
 

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
When posting code samples, use the
PHP:
, [html] or [code] tags as appropriate. They will separate the code from the rest of the post and preserve indenting, which is extremely important in Python.

You don't need two [URL="http://en.wikipedia.org/wiki/Shebang_(Unix)"]shebang[/URL] lines. The second will do nothing. The "#!/usr/bin/env python" is probably more portable, so use that one.

[quote="bryanda, post: 556423"]I would like to call the function hi() using
[URL]http://sllib.x10hosting.com/sl_py/hello2.py/hi[/URL],
 
but the word "hello" didn't appear.[/quote]
There's nothing in the script you posted that calls function [FONT="Courier New"]hi[/FONT]. 

[code]#!/usr/bin/env python
 
print "Content-type: text/plain\n\n"
 
def hi():
    print "hello"

hi()
[/code]

Note that in Python 3, print is no longer a statement and you need to call it as a function, thusly: '[FONT="Courier New"]print("hello")[/FONT]'.

Python doesn't have native support for URIs; there's no connection between the URI and the Python code. If you want to call a function based on the URI, you'll have to explicitly handle the URI.
say.py:
[code]#!/usr/bin/env python

import cgi, cgitb, os, re
#cgitb.enable()

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

def hi():
    print("hello")

def bye():
    print("goodbye")

actions={
  'hi': lambda: hi(), 
  'bye': lambda: bye()
}

def dispatch(path_info):
    matches=re.search('^/?([^/]+)', path_info or '/hi')
    action=matches.group(1)
    try:
        actions[action]()
    except KeyError:
        print "Illegal action:", action

dispatch(os.environ.get('PATH_INFO'))
[/code]

And here's a version that can handle query string parameters. Try open the page as "/cgi-bin/say2.py/bye" and "/cgi-bin/say2.py/bye?name=Dinsdale"
say2.py:
[code]#!/usr/bin/env python
import cgi, os, re

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

def hi(name=None):
    if name:
        print 'hello,', name
    else:
        print "hello"

def bye(name=None):
    if name:
        print 'hello,', name
    else:
        print "hello"

actions={
  'hi': lambda kwargs: hi(**kwargs), 
  'bye': lambda kwargs: bye(**kwargs)
}

def dispatch(path_info):
    matches=re.search('^/?([^/]+)', path_info or '/hi')
    action=matches.group(1)
    try:
        form=cgi.FieldStorage()
        actions[action](dict([(k,form[k].value) for k in form]))
    except KeyError:
        print("Illegal action:", action)
    

dispatch(os.environ.get('PATH_INFO'))
[/code]

[quote="bryanda, post: 556423"]but there is error and i think is the "mod_python" not found. How should i install the mod_python and if needed how should i create a support ticket?[/quote]
The X10 servers don't use mod_python and you can't install it. You could open a support ticket to request it, but mod_python is not necessary.
 

acafon

New Member
Messages
1
Reaction score
0
Points
0
I am using free hosting on x10.

.httaccess have "AddHandler cgi-script .cgi .py .pl .pm"

Trying to run python script directly but I get:
HTML:
Internal Server Error

The server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator, webmaster@insider8.exofire.net and inform them of the time the error occurred, and anything you might have done that may have caused the error.

More information about this error may be available in the server error log.

Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.

Python script test.py is very simple:
PHP:
#!/usr/bin/env python

print "Content-type: text/plain\n\n"
print "hello"

And it is encoded well in utf-8 and it runs on windows. Also tried it with #!/usr/bin/python but no effect.

I am on server "starka". Is python installed for this server or what? When I take a look in file manager, python script is detected as plain text/x-generic file. I also run some php files on the same account, is this maybe the cause?

Tried everything that I had found on this forum but nothing helps.

I can't track the problem, please help.
 

ah-blabla

New Member
Messages
375
Reaction score
7
Points
0
Sorry, got mixed up with another hosting service.

However regarding python working: it seems you have to place the script in the cgi-bin directory, i.e. go the folder where everything that is served to the web is, and you should find a directory called cgi-bin, place the script in there. Next make sure the file is executable (set through file permissions through ftp.) Now you can call it using http://yourdomain.bla/cgi-bin/script.py
 
Last edited:

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
It's possible to have CGI scripts outside of cgi-bin (see 2nd link in previous post), but requires jumping through more configuration hoops. Anyway, we don't want to reward threadjacking.
 

xav0989

Community Public Relation
Community Support
Messages
4,467
Reaction score
95
Points
0
Misson, I have a question, how do you execute a script from outside cgi-bin? I would find it quite useful!
 
Last edited:

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
python.py, chmod to 0755

Code:
#!/usr/bin/python
print "Content-type: text/plain\n\n"
print( "HI" )

.htaccess , add

Code:
AddHandler cgi-script  .py
 

xav0989

Community Public Relation
Community Support
Messages
4,467
Reaction score
95
Points
0
Ah... never though of that!
 
Top