Skip to content

Instantly share code, notes, and snippets.

@pajowu
Last active August 29, 2015 14:27
Show Gist options
  • Save pajowu/019f6a83aaa22d3b4885 to your computer and use it in GitHub Desktop.
Save pajowu/019f6a83aaa22d3b4885 to your computer and use it in GitHub Desktop.
Small proxy for CORS problems
FROM redis:latest
RUN apt-get -y update && apt-get install -y python python-pip
RUN pip install tinydb requests redis
ADD https://gist.githubusercontent.com/pajowu/019f6a83aaa22d3b4885/raw/proxy.py /proxy/
COPY urls.txt /proxy/
CMD redis-server & cd /proxy && python proxy.py
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
from cgi import parse_qs
import requests
import base64
import datetime
import redis
#from cache_times import CACHE_MIN # Dir with the TTL for each URL
FILTER = False # only for dev
CACHE_MIN = 5 # only for dev
if FILTER == True:
with open('urls.txt') as f:
VALID_URLS = f.read().splitlines()
f.close()
REDIS = redis.Redis()
def app(environ, start_response):
start_response('200 OK', [('Content-Type', 'text/html'), ('Access-Control-Allow-Origin', '*')])
parameters = parse_qs(environ.get('QUERY_STRING', ''))
url = "No url given"
if 'url' in parameters:
url = parameters['url'][0]
valid = True
if FILTER == True:
if url not in VALID_URLS:
valid = False
if valid:
return getDocByUrl(url)
print "ERROR: REQUEST COULD NOT BE MADE!"
return ""
def getDocByUrl(url):
cached = False
ago = datetime.datetime.now() - datetime.timedelta(minutes=CACHE_MIN)
ago = int((ago-datetime.datetime(1970,1,1)).total_seconds())
content = REDIS.get(url)
if content == None:
req = requests.get(url)
content = req.content
date = int((datetime.datetime.now()-datetime.datetime(1970,1,1)).total_seconds())
REDIS.setex(url,base64.b64encode(content),CACHE_MIN*60) # CACHE_MIN[url]
print "URL: {0} was inserted in cache with TTL: {1}!".format(url, CACHE_MIN) # CACHE_MIN[url]
else:
print "URL: {0} was found in cache!".format(url)
content = base64.b64decode(content)
return content
if __name__ == '__main__':
from wsgiref.simple_server import make_server
url = ''
port = 8081
srv = make_server(url, port, app)
print "Proxy-Server listening on {0}:{1}".format(url, port)
srv.serve_forever()
redis==2.10.3
requests==2.7.0
@passiweinberger
Copy link

die proxy url ist dann einfach der path in dem ichs starte:8080 ,oder?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment