Responding to web request with python

Christopher David Kyle cdkyle at ucalgary.ca
Mon Nov 3 00:40:14 EST 2008


On Sun, 2 Nov 2008, Tim Roberts wrote:

> scott212 <busitones at gmail.com> wrote:
> >
> >I'm responding with xml to a web request from google checkout but I
> >think I'm in a catch-22. To get my webserver (apache) to respond I
> >need a header and then a blank line before my xml begins, or else it
> >throws the 500 error. If have the blank line before the xml, the
> >service I'm responding to cannot parse it. If it's a response, can I
> >still use urllib or something like that? Is that my answer?
> 
> You must be misunderstanding something.  The HTTP protocol REQUIRES that
> the headers and the content be separated by a blank line.
> 
> Perhaps you should try to use a debug proxy to intercept the exact text of
> the response, just to make sure you're sending what you think you are
> sending.
> -- 
> Tim Roberts, timr at probo.com
> Providenza & Boekelheide, Inc.
> 

I agree with Tim, check the output. I had the same issue while serving up 
GIF images generated on the fly. I was sending two blank lines before the 
image data since an extra newline was being automatically added by the 
"print" statement.

# Print the header information
print "Content-type: image/gif\n\n"

The above fails since it sends: "header,newline,newline,newline" with the 
last newline appended by the print statement. A solution would have been 
to simple remove one of the my coded newline characters and let the print 
statement add the second one. However, I switched to "write" statements so 
I could _see_ the newline characters in my code.

import sys
# Write out the header information
sys.stdout.write("Content-type: image/gif\n\n")

The above worked because "write" does not add an automatic newline 
character to the output.

Also, make sure your content-type is correct. The same content would be 
handled differently by a browser depending on how it is identified.

"Content-type: text/html" - Sent through the HTML parser then displayed.
"Content-type: text/plain" - Displayed without formatting.

What is the application you're communicating with looking for?

Hope that helps,
Christopher





More information about the Python-list mailing list