Пример #1
0
import wsgiref
import urllib2
import os,sys
import timeout_urllib2; timeout_urllib2.sethttptimeout(4.0)
from sane_re import *

import tchelpers

os.system("mkdir -p httpcache.tc")
data_by_url = tchelpers.open("httpcache.tc/data_by_url.tch")
last_update = tchelpers.open("httpcache.tc/last_update.tch")

def application(environ, start_response):
  response_headers = [('Content-type', 'text/plain')]
  #start_response(status, response_headers)

  url = wsgiref.util.request_uri(environ)
  print url
  url = _S(url)['/(http.*)', 1]
  url = url.replace('http%3A//','http://')
  print url
  if url in data_by_url:
    print "CACHE HIT"
    data = data_by_url[url]
  else:
    try:
      print "URL FETCH"
      f = urllib2.urlopen(url)
      data = f.read()
      data_by_url[url] = data
    except (urllib2.URLError, timeout_urllib2.Error, socket.error), e:
Пример #2
0
# papers over the weird vagaries of urllib2 etc in python <=2.5 (2.6 is cleaner)

import urllib2, socket, timeout_urllib2; timeout_urllib2.sethttptimeout(5.0)
import urllib
from urllib2 import *
url_exceptions = (urllib2.URLError, timeout_urllib2.Error, socket.error)

import util

def urlencode(dct):
  dct = dict( (k, util.stringify(v)) for k,v in dct.iteritems())
  return urllib.urlencode(dct)
  
Пример #3
0
# papers over the weird vagaries of urllib2 etc in python <=2.5 (2.6 is cleaner)

import urllib2, socket, timeout_urllib2
timeout_urllib2.sethttptimeout(5.0)
import urllib
from urllib2 import *
url_exceptions = (urllib2.URLError, timeout_urllib2.Error, socket.error)

import util


def urlencode(dct):
    dct = dict((k, util.stringify(v)) for k, v in dct.iteritems())
    return urllib.urlencode(dct)
Пример #4
0
import sys, os, urllib2
from django.http import HttpResponse, HttpResponseRedirect
from django.template import loader, RequestContext
import simplejson
import re
from django.shortcuts import render_to_response
import trends, myurl, util
import timeout_urllib2

timeout_urllib2.sethttptimeout(20.0)


_IPHONE_UA = re.compile(r"Mobile.*Safari")


def is_iphone(request):
    return _IPHONE_UA.search(request.META["HTTP_USER_AGENT"]) is not None


def response(template, *args):
    def _process_view(view_func):
        def _handle_request(request, *args, **kwargs):
            context = view_func(request, *args, **kwargs)
            if context is None:
                context = dict()
            if isinstance(context, dict):
                path = is_iphone(request) and "iphone" or "web"
                return render_to_response("%s/%s" % (path, template), RequestContext(request, dict=context))
            else:
                return context  # this is probably a redirect
Пример #5
0
import sys, os, urllib2
from django.http import HttpResponse, HttpResponseRedirect
from django.template import loader, RequestContext
import simplejson
import re
from django.shortcuts import render_to_response
import trends, myurl, util
import timeout_urllib2
timeout_urllib2.sethttptimeout(20.0)

_IPHONE_UA = re.compile(r'Mobile.*Safari')


def is_iphone(request):
    return _IPHONE_UA.search(request.META['HTTP_USER_AGENT']) is not None


def response(template, *args):
    def _process_view(view_func):
        def _handle_request(request, *args, **kwargs):
            context = view_func(request, *args, **kwargs)
            if context is None: context = dict()
            if isinstance(context, dict):
                path = is_iphone(request) and 'iphone' or 'web'
                return render_to_response(
                    '%s/%s' % (path, template),
                    RequestContext(request, dict=context))
            else:
                return context  #this is probably a redirect

        return _handle_request