我有http代理项目。我使用一些重写规则将所有操作和 href 更改为 this /follow/+URL
。我还将所有 src 的 URL 更改为 this /proxy/+URL
,它可以帮助我在链接和例如图像之间分配 URL。虽然它适用于<img>
标签。我遇到的问题在于<iframe src=...>
标签。在我应用我的代码后,它会将 src 更改为 '/proxy/+URL' 但我想将其更改为/follow/+URL
,但我不确定我该怎么做。
这是我的代码
from urlparse import urlparse, urlunparse
import re, urllib
REWRITE_LINKS = re.compile(r'((?P<attr>action|href|src)=["\'](?P<uri>\S+?)["\'])', re.IGNORECASE)
def rewrite_links(content, mimetype = '', uri = ''):
uri = str(uri)
urip = urlparse(uri)
server_root = str(urlunparse((urip[0], urip[1], '/', '', '', '')))
working_dir = str(urlunparse((urip[0], urip[1], urip[2], '/', '', '')))
working_dir = '/'.join(working_dir.split('/')[:-1])
def repl_html(match):
attr, value = match.groupdict()['attr'], match.groupdict()['uri']
if value in ('',) or value.startswith('javascript:') or value.startswith('#'):
pass
else:
if value.find('://') == -1:
if value.startswith('./'):
value = working_dir + value[2:]
elif value.startswith('../'):
value = '/'.join(working_dir.split('/')[:-1]) + value[3:]
elif value.startswith('/'):
value = server_root + value[1:]
else:
value = server_root + value
#value = value.replace('/','|')
if attr.lower() == 'src':
value = '/proxy/' + value
else:
value = '/follow/' + value
return ' %s="%s" ' % (attr, value)
if mimetype.startswith('text/html'):
content = REWRITE_LINKS.sub(repl_html, content)
elif mimetype.startswith('text/css'):
pass
elif mimetype.startswith('application/x-javascript'):
pass
else:
pass
return content
你有什么建议吗?