summaryrefslogtreecommitdiff
path: root/http/pic/fetch.py
blob: 0cc3556f46e74f92f2c348367a61c8356d5bb1a2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# 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