Created
July 22, 2013 00:15
-
-
Save fuzeman/6050511 to your computer and use it in GitHub Desktop.
iceJabbR origin proxy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import traceback | |
from flask import Flask, Response, request | |
import requests | |
app = Flask(__name__) | |
STRIP_HEADERS = [ | |
'connection', | |
'content-encoding', | |
'content-length', | |
'vary' | |
] | |
@app.route('/trakt/<path:path>') | |
def trakt(path): | |
try: | |
path = 'http://api.trakt.tv/' + path | |
print '[TRAKT]', path | |
forwardedFor = request.headers.get('X-Forwarded-For') + ", " + request.remote_addr \ | |
if 'X-Forwarded-For' in request.headers \ | |
else request.remote_addr | |
print '\tX-Forwarded-For:', forwardedFor | |
try: | |
r = requests.get(path, timeout=3) | |
print '[RESPONSE]', '(' + str(r.status_code) + ')' | |
headers = r.headers | |
headers['Access-Control-Allow-Origin'] = '*' | |
headers['X-Forwarded-For'] = forwardedFor | |
# Strip headers we don't need | |
for h in STRIP_HEADERS: | |
if h in headers: | |
headers.pop(h) | |
return Response(r.text, r.status_code, headers.items()) | |
except requests.exceptions.ConnectionError: | |
return Response('', 504, {}) | |
except requests.exceptions.Timeout: | |
return Response('', 504, {}) | |
except Exception, ex: | |
print '[EXCEPTION]', ex | |
traceback.print_exc() | |
raise ex | |
if __name__ == '__main__': | |
port = int(os.environ.get("PORT", 5000)) | |
app.run(host='0.0.0.0', port=port) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
web: python icejabbr-origin.py |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment