[mod_python] Problems with headers in proxy handler

Graham Dumpleton grahamd at dscpl.com.au
Fri Dec 9 23:17:00 EST 2005


Which version of mod_python are you using?

On 08/12/2005, at 12:14 AM, Sebastjan Trepca wrote:

> Hi,
>
> I'm writing a proxy handler which makes a request to a different page
> and retrieves its content and headers. This is the source:
>
> import urllib2
> from mod_python import util
>
> def handler(req):
>     req.headers_out.clear()
>     f = util.FieldStorage(req)
>     url = f.get('url',None)
>     if(url):
>         url=url.split('?')
>         if(len(url)>1):
>             u = url[0]
>             p = url[1]
>             ureq = urllib2.Request(u,p)
>         else:
>             u = url[0]
>             ureq = urllib2.Request(u)
>         for h in req.headers_in:
>             ureq.add_header(h,req.headers_in[h])
>         r = urllib2.urlopen(ureq)
>         for h in r.headers:
>             req.headers_out[h]=r.headers[h]
>         data =     r.read()

In case you are using mod_python 2.7.X, add at this point:

            req.send_http_header()

>         req.write(data)
>         return 0#-2 doesn't help either
>     return 0

Will not make a difference, but don't return 0/2, use apache.OK/ 
apache.DONE
instead. Using literal integer values is bad programming practice.

> But this seems to have problems with headers, I get back basic headers
> from my Apache server, which is wrong of course.
> Any ideas why is this happening?

Graham


More information about the Mod_python mailing list