首页 文章

在浏览器开发工具中停止网络请求

提问于
浏览
2

我正在观察chrome dev工具中的一系列重定向,“网络”选项卡:

enter image description here

我需要能够在“网络”中出现请求后暂停重定向链(以便我可以将其复制为cURL)但在执行之前 . 像“暂停任何网络活动”之类的东西 . 我在Chrome开发工具,Firefox Web开发人员,Firebug,Safari中搜索过此功能,但无济于事 . 最接近的是萤火虫中的“暂停XHR”,但这些重定向不是XHR .

我会接受一个非浏览器解决方案(脚本?),如果它能完成这项工作,虽然我觉得这应该可以通过浏览器开发工具实现 .

1 回答

  • 1

    我没有使用非浏览器解决方案,有一个python脚本(它也使用 requests 库)跟随重定向,直到找到一些后缀并打印cURL请求 .

    #!/usr/bin/env python
    
    import requests 
    import sys
    
    def formatRequestAscURL(request):
        command = "curl -X {method} -H {headers} -d '{data}' '{url}'"
        method = request.method
        url = request.url
        data = request.body
        headers = ["{0}: {1}".format(k, v) for k, v in request.headers.items()]
        headers = " -H ".join(headers)
        return command.format(method=method, headers=headers, data=data, url=url)
    
    
    def followUntilSuffix(startURL, suffix):
        response = requests.get(startURL, allow_redirects=False)
        session = requests.Session()
        requests_iter = session.resolve_redirects(response, response.request)
    
        for r in requests_iter:
            if r.request.path_url.endswith(suffix):
                 print formatRequestAscURL(r.request)
                 return
    
        print 'Required redirect isn\'t found'
    
    
    if len(sys.argv) < 3:
        print 'This script requires two parameters:\n 1) start url \n 2) url suffix for stop criteria'
        sys.exit()
    
    startURL = sys.argv[1]
    stopSuffix = sys.argv[2]
    
    followUntilSuffix(startURL, stopSuffix)
    

相关问题