#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Posts
    136
    Rep Power
    11

    CGI form question


    Just a question....

    Is there a way that

    Code:
    <FORM TYPE="POST" ACTION="file.cgi">

    can be


    Code:
    <FORM TYPE="POST" ACTION="python_function_name">
    ?
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    206
    Rep Power
    12
    I think if there's only one function in test.py it is possible to call that function like this:
    Code:
    <form action="cgi-bin/test.py">
     ...
     <input type="submit">
    ...
    </form>
    But to call a function by it's name from a certain module ..hmmm ... no idea.
  4. #3
  5. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    13
    No - not directly. You can always pass the name of the function you want to execute as a hidden value.

    Here are several ways to have equivalent functionality:

    Code:
    def funcA():
        #...
    def funcB():
        #...
    launch = {"Call A":funcA, "Call B":funcB}
    
    #Call the function:
    launch[hidden_value_from_form]()
    Code:
    def funcA():
        #...
    def funcB():
        #...
    if hidden_value_from_form == 'Call A':
        funcA()
    elif hidden_value_from_form == 'Call B':
        funcB()
    Code:
    #dangerous....
    #hidden_value_from_form takes values such as 'funcA' and 'funcB'
    def funcA():
        #...
    def funcB():
        #...
    eval("%s()"%hidden_value_from_form)
    grimey
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2004
    Location
    Regensburg, Germany
    Posts
    147
    Rep Power
    16
    Works only for apache http servers if you are able to use .htaccess files:

    Put the following lines into the .htacess file:

    RewriteEngine On
    RewriteBase /
    RewriteRule ^([a-z_]*)$ file.cgi?$1=1 [L]


    Apache will translate the request
    http://my_domain.com/function_name
    to
    http://my_domain.com/file.cgi?function_name=1

    In your cgi script, you only need to check if the function name is set to 1.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Posts
    136
    Rep Power
    11
    Ok thanks grimey!
    was hoping it was possible to do it directly so I wouldnt need using more than 1 file, but I guess its not that much of a problem doing it that way...

    Dag
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Posts
    136
    Rep Power
    11
    sbkwi: okey, but I'm not using apache.... I will note down anyways though(maybe I'll use it on some computer later), thank you

IMN logo majestic logo threadwatch logo seochat tools logo