0

I'm building a site and attempting to do everything I used to do in PHP in Python (what can I say, I just like it better!). There are a couple of scenarios that I don't quite understand, however. Such as:

1. If my plain ol' Python page is called from an Ajax function and returns some json, how do I control the headers that are sent back? In PHP I used the header() function where I don't see an obvious equivalent in Python.

2. If I want to provide a download for a user and I want it to live outside th webroot for security purposes, how do I read that binary file and put it out to the output buffer? In PHP it was the fpassthru() function.

Thanks everyone, I really appreciate your help!

2
Contributors
1
Reply
2
Views
8 Years
Discussion Span
Last Post by [Wacky]
0

Hi, I'm just a Python newbie, but since no-one else has replied, I'll throw a few suggestions out there for you. :)

I'm building a site and attempting to do everything I used to do in PHP in Python (what can I say, I just like it better!).

That same idea is becoming more and more appealing to me, too!

There are a couple of scenarios that I don't quite understand, however. Such as:

1. If my plain ol' Python page is called from an Ajax function and returns some json, how do I control the headers that are sent back? In PHP I used the header() function where I don't see an obvious equivalent in Python.

I know very little about web-based Python, but if by "plain old Python" you mean CGI Python, then simply printing the appropriate HTTP headers should do the job. It's all header() ever really did, after all. :D

Example for UTF-8-encoded XML:

# I forget whether IE is picky about which MIME types it will accept
# for XMLHttpRequest responses... YMMV.
print "Content-Type: application/xml; charset=UTF-8"
# And a blank line to end the HTTP headers section, just before
# you output your XML. Any other headers (e.g. Content-Length)
# must precede this.
print
# And finally, print your XML here.

You get the idea. :)

There's probably already a cgi.something_to_do_with_headers() raft of functions/objects in the Python standard library, but you could always write a simple convenience function if there's nothing built in.

If you're using mod_python or something else, then I'm afraid that I have no idea, but I believe the mod_python environment has helpers for this sort of thing. "Consult the docs" is all I can suggest for that.

2. If I want to provide a download for a user and I want it to live outside the webroot for security purposes, how do I read that binary file and put it out to the output buffer? In PHP it was the fpassthru() function.

Now this one I have even less of a clue about... Here's a probably highly unpythonic and misguided attempt, for CGI Python:

import os.path

your_file_path = '/etc/hosts'
your_file_name = os.path.basename(your_file_path)

print "Content-Type: application/octet-stream"
print "Content-Length: %s" % str(os.path.getsize(your_file_path))
print "Content-Disposition: attachment; filename=%s" % your_file_name
print "Content-Transfer-Encoding: binary" # Change as appropriate
print # Our blank-line friend

# This is where it gets really dodgy. There are probably all sorts of
# buffer issues and whatnot lurking and ready to strike...
your_file = open(your_file_path, 'rb')
print your_file.read(), # Note the comma! I think the auto-newline that
# Python's print statement adds could break things... Not 100% sure.

It's probably horribly broken and wrong, but I've never used Python on the web (though I plan to soon), so that's my defence if so. :D In any case, it might give you a starting point.

I hope I've helped at least a little.

Paul

Edit: It didn't seem to make any difference in a quick test I did, but I changed the read-mode to 'rb' (binary). Again, change as appropriate...

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.