[Tutor] Fw: Apache, CGI-BIN, Python
Eric Brunson
brunson at brunson.com
Tue Sep 11 10:31:34 CEST 2007
Eric Brunson wrote:
> ALAN GAULD wrote:
>
>> Forwarding to the list....
>>
>> NB Use Reply-All when replying to the tutor list.
>>
>> Alan G.
>>
>> ----- Forwarded Message ----
>> From: Ryan <silas428 at gmail.com>
>> To: Alan Gauld <alan.gauld at btinternet.com>
>> Sent: Tuesday, 11 September, 2007 5:32:33 AM
>> Subject: Re: [Tutor] Apache, CGI-BIN, Python
>>
>> Alan Gauld wrote:
>>
>>
>>> "Ryan" <silas428 at gmail.com> wrote
>>>
>>>
>>>
>>>
>>>> I am running a Linux box and cannot find my Apache cgi-bin to put
>>>> some
>>>> python scripts in. I know I probably have to create one but don't
>>>> know
>>>> where and how.
>>>>
>>>>
>>>>
>>> I think there are several places that you can create cgi-bin depending
>>> on how you configure apache. But the place I usually see it is
>>> directly
>>> under the apache root directory - ie the one that is equivalent to /
>>> in
>>> the url.
>>>
>>>
>>>
>>>
>>>> everything works fine except:
>>>>
>>>> form = cgi.FieldStorage()
>>>>
>>>> #This is where everything goes wrong
>>>> #I get error messages for either the lastname line or firstname
>>>>
>>>>
>>>>
>>> Can you tell us
>>> a) exactly what error messages and
>>> b) How you are invoking the script - where are the values
>>> supposed to be coming from?
>>>
>>>
>>>
>>>
>>>> lastname = form['lastname'].value
>>>> firstname =form['firstname'].value
>>>> message = firstname + " " + lastname
>>>> print reshtml % message
>>>>
>>>>
>>>>
>>> The HTML Doc:
>>>
>>>
>>>
>> <html>
>>
>> <form action = "test1.py">
>> <p> Enter your first name:</p>
>> <input type="text" name="firstname" size="40">
>> <input type="text" name="lastname" size="40">
>> <input type = "submit">
>> <input type ="reset">
>> </form>
>> </html>
>>
>> Error:
>>
>> Mod_python error: "PythonHandler mod_python.publisher"
>>
>>
>
> You don't use the cgi module when using the mod_python publisher
> handler. Your function will be called with the form variables as named
> parameters, like this:
>
> def handle_form_input( req, firstname, lastname ):
> # do something here
>
> I would recommend giving defaults for the parameters so if the user
> doesn't submit a value it, it doesn't throw an exception, like this:
>
> def handle_form_input( req, firstname=None, lastname=None ):
> # whatever
>
> And, I've been known to simulate the cgi behavior like this:
>
> def handler_form_input( req, **form ):
> # access your form variables as an array
> print "You entered %s %s" % ( form[lastname], form[firstname] )
>
>
It's late, that should read:
def handler_form_input( req, **form ):
# access your form variables as a dict
return "You entered %s %s" % ( form['lastname'], form['firstname'] )
> But, remember that this "form" is a simple dict, not a cgi.FieldStorage
> and I don't know what it does with multiple values with the same key.
> FieldStorage puts them in an array, you'd have to experiment with this
> in the publisher handler.
>
> See how much better answers you get when you ask good questions and
> include error messages. :-)
>
> Hope that helps,
> e.
>
>
>
>> Traceback (most recent call last):
>>
>> File "/usr/lib/python2.5/site-packages/mod_python/apache.py", line 299, in HandlerDispatch
>> result = object(req)
>>
>> File "/usr/lib/python2.5/site-packages/mod_python/publisher.py", line 204, in handler
>> module = page_cache[req]
>>
>> File "/usr/lib/python2.5/site-packages/mod_python/cache.py", line 82, in __getitem__
>> return self._checkitem(name)[2]
>>
>> File "/usr/lib/python2.5/site-packages/mod_python/cache.py", line 124, in _checkitem
>> value = self.build(key, name, opened, entry)
>>
>> File "/usr/lib/python2.5/site-packages/mod_python/publisher.py", line 77, in build
>> return ModuleCache.build(self, key, req, opened, entry)
>>
>> File "/usr/lib/python2.5/site-packages/mod_python/cache.py", line 371, in build
>> exec opened in module.__dict__
>>
>> File "/var/www/python/test1.py", line 16, in
>> lastname = form['lastname'].value
>>
>> File "cgi.py", line 567, in __getitem__
>> raise KeyError, key
>>
>> KeyError: 'lastname'
>>
>> But I can get this script to work properly:
>> def index(req):
>> return "Test succesful";
>>
>>
>>
>>
>>
>>> HTH,
>>>
>>>
>>>
>>>
>>>
>>
>>
>> _______________________________________________
>> Tutor maillist - Tutor at python.org
>> http://mail.python.org/mailman/listinfo/tutor
>>
>>
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
More information about the Tutor
mailing list