tags:

views:

52

answers:

2

How to send a raw http header in python just like header() in PHP ?

A: 

Pass a list of two-tuples containing the header name and header value to the start_response() function.

Ignacio Vazquez-Abrams
This is for WSGI applications.
Epeli
+1  A: 

In Django, you'd be like:

def someview(request):
    # ... etc ...
    out = HttpResponse(outputstring,
        mimetype="text/html",
        status_code="302",
        )
    out['Content-Disposition'] = "attachment; filename=download.html"
    # fill in all your favorite HTTP headers here
    return out

... for cache-control and friends, you need to import a bunch of decorators and wrap your view functions accordingly (I forget which) -- this is because django has a caching system with which many sub-rosa bits of the framework are integrated.

I find the cache stuff confusing, but also nice. non-cache HTTP headers are E-Z.

fish2000
I just use print 'Location: http://'
bandw
that probably works -- but I know that the django people think that's a bad idea as they redefine sys.stdout and friends. using `print` for headers will probably break at some point.
fish2000