mitmproxy v0.18.2版本Python script示例


记录请求日志到MongoDB

  1. # 记录请求日志到MongoDB  
  2. import pymongo  
  3. from datetime import datetime  
  4. from mitmproxy import ctx  
  5.   
  6. # 连接MongoDB  
  7. client = pymongo.MongoClient()  
  8. db = client['mitmproxy']  
  9. collection = db['logs']  
  10.   
  11. def request(flow):  
  12.     ctx.log.info("-->" + flow.request.url)  
  13.     #https://mitmproxy.readthedocs.io/en/v2.0.2/_modules/mitmproxy/net/http/request.html  
  14.     collection.insert({'url': flow.request.url,   
  15.                     'method': flow.request.method,  
  16.                     'headers': flow.request.headers,   
  17.                     'form': flow.request.urlencoded_form or flow.request.multipart_form,  
  18.                     'datetime': datetime.now()})  
# 记录请求日志到MongoDB
import pymongo
from datetime import datetime
from mitmproxy import ctx

# 连接MongoDB
client = pymongo.MongoClient()
db = client['mitmproxy']
collection = db['logs']

def request(flow):
    ctx.log.info("-->" + flow.request.url)
    #https://mitmproxy.readthedocs.io/en/v2.0.2/_modules/mitmproxy/net/http/request.html
    collection.insert({'url': flow.request.url, 
                    'method': flow.request.method,
                    'headers': flow.request.headers, 
                    'form': flow.request.urlencoded_form or flow.request.multipart_form,
                    'datetime': datetime.now()})

 

拦截(屏蔽)某个请求

  1. # 拦截百度https://www.baidu.com/link?url=,返回404  
  2. import re  
  3. from mitmproxy import ctx  
  4. from mitmproxy.models import HTTPResponse  
  5.       
  6. def request(flow):  
  7.     """修改请求 
  8.     """  
  9.     if re.compile(r'/link\?url=.*').search(flow.request.url):  
  10.         ctx.log.info(" --->" + flow.request.url)  
  11.         # 返回404  
  12.         flow.response = HTTPResponse.make(404)  
# 拦截百度https://www.baidu.com/link?url=,返回404
import re
from mitmproxy import ctx
from mitmproxy.models import HTTPResponse
    
def request(flow):
    """修改请求
    """
    if re.compile(r'/link\?url=.*').search(flow.request.url):
        ctx.log.info(" --->" + flow.request.url)
        # 返回404
        flow.response = HTTPResponse.make(404)

 

重写一个域名

  1. # 将www.baidu.com域名改写为www.site-digger.com  
  2. from mitmproxy import ctx  
  3.   
  4. def request(flow):   
  5.     if flow.request.pretty_host.endswith('www.baidu.com'):   
  6.         flow.request.host = 'www.site-digger.com'   
  7.         flow.request.scheme = 'http'   
  8.         flow.request.port = 80   
  9.         ctx.log.info(" --->" + flow.request.url)   
# 将www.baidu.com域名改写为www.site-digger.com
from mitmproxy import ctx

def request(flow): 
    if flow.request.pretty_host.endswith('www.baidu.com'): 
        flow.request.host = 'www.site-digger.com' 
        flow.request.scheme = 'http' 
        flow.request.port = 80 
        ctx.log.info(" --->" + flow.request.url) 

 

修改应答数据

  1. # 将百度首页的logo替换掉  
  2. import re  
  3. from mitmproxy import ctx  
  4.       
  5. def response(flow):  
  6.     if flow.request.pretty_host.endswith('www.baidu.com'and flow.request.path.endswith('/'):  
  7.         flow.response.text = re.sub(r'//www\.baidu\.com/img/bd_logo[^\"]+''http://www.site-digger.com/images/logo.png', flow.response.text)  
# 将百度首页的logo替换掉
import re
from mitmproxy import ctx
    
def response(flow):
    if flow.request.pretty_host.endswith('www.baidu.com') and flow.request.path.endswith('/'):
        flow.response.text = re.sub(r'//www\.baidu\.com/img/bd_logo[^\"]+', 'http://www.site-digger.com/images/logo.png', flow.response.text)

 

特别说明:本文旨在技术交流,请勿将涉及的技术用于非法用途,否则一切后果自负。如果您觉得我们侵犯了您的合法权益,请联系我们予以处理。
☹ Disqus被Qiang了,之前所有的评论内容都看不到了。如果您有爬虫相关技术方面的问题,欢迎发到我们的问答平台:http://spider.site-digger.com/
posted @ 2020-11-23 16:23  逐梦~前行  阅读(218)  评论(0编辑  收藏  举报