Форум сайта python.su
Надо пробовать.
Офлайн
А как этот paste ставить?
Офлайн
asilyator
zheromo, а можно так сделать вот что http://python.su/forum/viewtopic.php?id=8901 ?
import os
try:
import cPickle as pickle
except ImportError:
import pickle
from paste.proxy import TransparentProxy
from webob import Request, Response
from webob.exc import HTTPForbidden
from hashlib import md5
CACHE_DIR = 'c:/temp/cache'
class SimpleProxy(object):
def __init__(self, proxy):
self.proxy = proxy
def do_proxy(self, req, environ, start_response):
req.remove_conditional_headers()
resp = req.get_response(self.proxy)
resp.decode_content()
return resp
def __call__(self, environ, start_response):
req = Request(environ)
resp = self.do_proxy(req, environ, start_response)
return resp(environ, start_response)
class FileIterable(object):
def __init__(self, filename):
self.filename = filename
def __iter__(self):
return FileIterator(self.filename)
class FileIterator(object):
chunk_size = 4096
def __init__(self, filename):
self.fileobj = open(filename, 'rb')
def __iter__(self):
return self
def next(self):
chunk = self.fileobj.read(self.chunk_size)
if not chunk:
raise StopIteration
return chunk
def OKValidator(resp):
return resp.status_int == 200
class CachedProxy(SimpleProxy):
def __init__(self, proxy, resp_validator=OKValidator):
super(CachedProxy, self).__init__(proxy)
self.resp_validator = resp_validator
def do_proxy(self, req, environ, start_response):
filename = os.path.join(CACHE_DIR, md5(req.url).hexdigest())
if os.path.exists(filename+'-head'):
print 'GET', req.url
return self._load_response(filename)
else:
resp = super(CachedProxy, self).do_proxy(req, environ, start_response)
if self.resp_validator and self.resp_validator(resp):
print 'PUT', req.url
self._save_response(filename, resp)
return resp
def _save_response(self, filename, resp):
pickle.dump(self._pickle_response(resp), open(filename+'-head','wb'), -1)
open(filename+'-body','wb').write(resp.body)
def _load_response(self, filename):
resp = self._unpickle_response(pickle.load(open(filename+'-head','rb')))
resp.app_iter = FileIterable(filename+'-body')
return resp
def _pickle_response(self, res):
return dict(status=res.status_int, headerlist=res.headerlist)
def _unpickle_response(self, value):
return Response(**value)
def ImageValidator(resp):
return resp.status_int == 200 and \
resp.content_type.split('/')[0] == 'image'
def main():
app = CachedProxy(TransparentProxy(), ImageValidator)
from paste.httpserver import serve
serve(app, 'localhost', 3127)
if __name__ == '__main__':
main()
Офлайн
asilyatorpip install paste
А как этот paste ставить?
Офлайн
Под виндой.
Уже наковырял easy_install, а как деинсталлить пакеты?
Отредактировано (Окт. 31, 2010 02:13:22)
Офлайн
мне больше нравится pip
easy_install pip
pip uninstall easy_install
:)
Офлайн