+ Reply to Thread
Results 1 to 9 of 9

Thread: Python

  1. #1
    bryanda is offline x10Hosting Member bryanda is an unknown quantity at this point
    Join Date
    Jun 2009
    Posts
    5

    Question Python

    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.

  2. #2
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: Python

    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 shebang lines. The second will do nothing. The "#!/usr/bin/env python" is probably more portable, so use that one.

    Quote Originally Posted by bryanda View Post
    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.
    There's nothing in the script you posted that calls function hi.

    Code:
    #!/usr/bin/env python
     
    print "Content-type: text/plain\n\n"
     
    def hi():
        print "hello"
    
    hi()
    Note that in Python 3, print is no longer a statement and you need to call it as a function, thusly: 'print("hello")'.

    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'))
    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'))
    Quote Originally Posted by bryanda View Post
    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?
    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.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

  3. #3
    acafon is offline x10Hosting Member acafon is an unknown quantity at this point
    Join Date
    Oct 2009
    Posts
    1

    Unhappy Re: Python

    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 Code:
    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 Code:
    #!/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.

  4. #4
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: Python

    Quote Originally Posted by acafon View Post
    Tried everything that I had found on this forum but nothing helps.
    Don't threadjack and learn to search.

    Quote Originally Posted by ah-blabla View Post
    Python isn't enabled for free hosting, so you won't be able to run the code on the server.
    Yes it is.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

  5. #5
    ah-blabla's Avatar
    ah-blabla is offline x10 Lieutenant ah-blabla is an unknown quantity at this point
    Join Date
    Sep 2009
    Posts
    375

    Re: Python

    Quote Originally Posted by misson View Post
    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 by ah-blabla; 10-13-2009 at 02:56 AM.

  6. #6
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: Python

    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.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

  7. #7
    xav0989's Avatar
    xav0989 is offline Community Public Relation xav0989 is just really nice
    Join Date
    Jul 2008
    Location
    ifk
    Posts
    4,438

    Re: Python

    Misson, I have a question, how do you execute a script from outside cgi-bin? I would find it quite useful!
    Last edited by xav0989; 10-13-2009 at 07:16 PM.
    Xavier L | Community Public Relations Manager (Free Hosting Support)
    █ Yes, my position is too cool to even exist!
    How am I helping? Rate this post by clicking the icon below! (this is even better than "liking" a post)
    Terms of Service | Acceptable Use Policy | x10Hosting Wiki

  8. #8
    descalzo's Avatar
    descalzo is offline Grim Squeaker descalzo has a brilliant futuredescalzo has a brilliant futuredescalzo has a brilliant future
    Join Date
    Jul 2009
    Location
    Ankh-Morpork
    Posts
    7,636

    Re: Python

    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
    Nothing is always absolutely so.

  9. #9
    xav0989's Avatar
    xav0989 is offline Community Public Relation xav0989 is just really nice
    Join Date
    Jul 2008
    Location
    ifk
    Posts
    4,438

    Re: Python

    Ah... never though of that!
    Xavier L | Community Public Relations Manager (Free Hosting Support)
    █ Yes, my position is too cool to even exist!
    How am I helping? Rate this post by clicking the icon below! (this is even better than "liking" a post)
    Terms of Service | Acceptable Use Policy | x10Hosting Wiki

+ Reply to Thread

Similar Threads

  1. Replies: 5
    Last Post: 07-10-2009, 03:30 AM
  2. MySQLdb on Python
    By hardbyte in forum Free Hosting
    Replies: 0
    Last Post: 11-25-2008, 02:03 AM
  3. Python / MySQLdb
    By Korexio in forum Free Hosting
    Replies: 9
    Last Post: 11-29-2007, 08:29 AM
  4. Python/.htaccess help
    By *Face in forum Free Hosting
    Replies: 6
    Last Post: 09-21-2007, 01:44 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers