Problems with POST

Jeff Davis jdavis at empires.org
Sun Feb 24 21:44:22 CET 2002


Can you run the second script as a cgi and load it with a web browser to 
determine which script is not behaving correctly? I have a suspicion that 
the first script is somehow not posting quite correctly.

Regards,
        Jeff

Sunit Joshi wrote:

> Hello
> Somehow I can't seem to get any data with POST although the same
> script (with modifications in the first one) works for GET . Here's
> the scripts: First one is to POST and next one to recieve the POSTED
> data.
> 
> #!C:\Python21\Python.exe -u
> #To POST and output the data - postTest.py
> 
> from httplib import HTTP
> from urllib import quote
> 
> quoteData = "Sunit Joshi"
> postData = 'txtName=' + quote(quoteData)
> 
> print 'Will POST ', len(postData), 'bytes:', postData
> 
> # begin HTTP request
> req = HTTP('sunitw2k:6080')
> req.putrequest("POST" ,'/PyCgi/formTest.py')
> req.putheader("Accept", "text/html")
> req.putheader("User-Agent", "postTest.py")
> 
> # Set Content-length to length of PostData
> req.putheader("Content-length", str(len(postData)))
> req.endheaders()
> 
> # Send POST data after ending headers
> req.send(postData)
> 
> replycode, message, headers = req.getreply()
> print "HTTP RESPONSE: ", replycode, message
> print 'HEADERS: ', str(headers)
> 
> # Get file-like object from HTTP response
> # and print received HTML to screen.
> fd = req.getfile()
> txtlines = fd.read()
> fd.close()
> print '\nReceived following HTML:\n'
> print txtlines
> 
> #************************
> #!c:\Python21\Python.exe -u
> # To send the data posted - formTest.py
> import sys
> import cgi
> 
> sys.stderr = sys.stdout
> 
> form = cgi.FieldStorage()
> print "Content-type: text/html"
> print
> if not form:
> print "<h2>Field 'txtName' not filled in.</h2>"
> print str(form)
> else:
> print '<h3>You entered <font color="#FF6600">%s</font></h3>' %
> form.getvalue('txtName','none')




More information about the Python-list mailing list