# coding=utf-8 from mod_python import apache import os, urllib2 CEZAR_URL = 'http://msc.com.pl/cezar' def handler(req): orig_req = req while True: if orig_req.prev: orig_req = orig_req.prev else: break remote_resource = CEZAR_URL + orig_req.uri request = urllib2.Request(remote_resource) request.add_header('User-Agent', orig_req.headers_in['User-Agent']) file_name = os.path.join(os.path.dirname(__file__), remote_resource.split('/')[-1]) try: response = urllib2.urlopen(request) open(file_name, 'w+').write(response.read()) req.content_type = response.headers['Content-Type'] req.write(open(file_name, 'r').read()) return apache.OK except urllib2.URLError: return apache.HTTP_NOT_FOUND