Skip to content

Instantly share code, notes, and snippets.

@RaD
Last active January 16, 2019 15:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save RaD/56e5f85460670f10445fe8cb3accaa48 to your computer and use it in GitHub Desktop.
Save RaD/56e5f85460670f10445fe8cb3accaa48 to your computer and use it in GitHub Desktop.
simple http proxy
# -*- coding: utf-8 -*-
import argparse
import re
import requests
from bs4 import BeautifulSoup, Comment
from flask import Flask
from flask import Response
app = Flask(__name__)
@app.route('/')
@app.route('/<path:url>')
def proxy(url=''):
url = '{}/{}'.format(args.target, url)
req = requests.get(url)
content_type = req.headers['content-type']
content = req.content
if 'text/html' in content_type:
soup = BeautifulSoup(content, 'html5lib')
for line in soup.find_all(string=re_marker):
if line.parent.name in ('script', 'style', 'code', '[document]'):
continue
if line.parent(text=lambda x: isinstance(x, Comment)):
continue
text = re.sub(u'\\b(\\w{{{}}})\\b'.format(args.symbols), u'\\1™', line, flags=re.U)
line.replace_with(text)
for link in soup.find_all('a', href=re_source):
link['href'] = link['href'].replace(args.target, 'http://{}:{}'.format(args.hostname, args.port))
content = str(soup)
return Response(content, content_type=req.headers['content-type'])
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='TM HTTP Proxy.')
parser.add_argument('--hostname', default='127.0.0.1', help='Default: 127.0.0.1')
parser.add_argument('--port', default='8232', help='Default: 8232')
parser.add_argument('--target', default='https://habrahabr.ru', help='Default: https://habrahabr.ru')
parser.add_argument('--symbols', default='6', help='Default: 6')
args = parser.parse_args()
re_source = re.compile(args.target, re.U)
re_marker = re.compile(u'.*', re.U)
print('Proxy for {}'.format(args.target))
app.run(host=args.hostname, port=int(args.port))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment