Problems with POST
Sunit Joshi
sjoshi at ingr.com
Sat Feb 23 09:14:14 EST 2002
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