Mitmproxy 配置二级代理

做爬虫时常遇到需要切换代理的情况,mitmproxy 对python有很好的支持,可以通过python脚本自定义二级代理的代理方式。

第一步 命令行启用upstream模式

注: 只有在一开始启动了upstream 模式, 后续才可以进行定制化跳转

mitmdump --mode upstream:http://defaule-upstream-proxy.local:8080/ -s ./change_upstream_proxy.py

第二步 mitmdump 脚本根据所需定义规则,跳转不同的二级代理

def request(flow:http.HTTPFlow) -> None:
    if flow.request.method == "CONNECT":
        # if the decision is done by domain, one could alse modify the server address here.
        # We do it after CONNECT here to have the reqeust data available as well.
        return
        client_ip = flow.client_conn.address[0]
        if 'xxx.com' in flow.request.url: 
            ctx.log.info(flow.request.url)
            proxy = ("localhost", 8888)
        else:
            proxy = ("localhost", 3800)

        #这里配置二级代理的IP地址和端口
     if flow.live:    
        flow.live.change_upstream_prxoy_server(proxy)

第三部 测试

可以通过访问 http://httpbin.org/get 或者http://httpbin.org/ip 来测试自己的代理ip是否成功。

 

原文地址:https://blog.51cto.com/leyex/2170340

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值