可以替换掉网页某个文件的代理

用twisted写一个替换掉网站上某个文件的代理,作用参见 http://lifesinger.org/blog/?p=40 “如何直接调试线上页面的JavaScript和CSS”这篇文章,有了这个就不用fidder了,而且使用更加方便,替换时把要把原来的文件的content-length这个header去掉:

from twisted.web import http

from twisted.web import proxy

from twisted.internet import reactor

from twisted.python import log

import sys

log.startLogging(sys.stdout)



rpl_urls ={

"http://g.cn/" : "g.cn.html",

}

urls = rpl_urls

#import config

#urls = config.rpl_urls



class rpl_Proxy(proxy.Proxy):

    def __init__(self):

        log.msg("hey, rpl_proxy init")

        proxy.Proxy.__init__(self)

        self.requestFactory = rpl_ProxyRequest



class rpl_ProxyRequest(proxy.ProxyRequest):

    def __init__(self, channel, queued, reactor=reactor):

        log.msg("hey, rpl_ProxyRequest init")

        proxy.ProxyRequest.__init__(self, channel, queued, reactor)



    def process(self):

        log.msg("url:",self.uri)

        if self.uri in urls:

            filename = urls[self.uri]

            fd = open( filename, "rb" )

            self.transport.write( fd.read() )

            self.transport.loseConnection()

        else:

            proxy.ProxyRequest.process(self)

      

class ProxyFactory(http.HTTPFactory):

    def __init__(self):

        http.HTTPFactory.__init__(self)

        self.protocol = rpl_Proxy



if __name__ == '__main__':

    """

    written by karlzheng; http://blog.csdn.net/zhengkarl

    """

    reactor.listenTCP(8080, ProxyFactory())

    reactor.run()



#http://wiki.python.org/moin/Twisted-Examples
http://bbs.superlinux.cn/viewthread.php?tid=2560&extra=page%3D1&frombbs=1

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值