Package: requests-debugger
requests_debugger.pyThis single file should be used to help in debugging your project that uses 'requests' libTable of ContentsWhy Should I Use This?When Should I NOT Use This?How ToFeaturesUsageSingleton moduleLocal moduleTraceback featureOutput formatsComplex requestsGetting the standard 'requests' backTODOWhy Should I Use This?Because you are tired of setting breakpoints or 'print's into your code to figure out what requests for what urls your project is making.
I did this cause I'm on a project that makes a lot of requests for lots of diferente places using several layers of abstraction.
With this I figured that I make about 600 requests every time I run a full project test and that some views make up to 25 requests to get done. Now I'm refactoring my abstractions to make less requests and everybody is happy :)When Should I NOT Use This?On your production environment. This guy is working fine but you dont need to insert this lame-hacking-failure-point into you production code, do you?How ToInstall withpip install requests-debugger-Then just import this lib withimportrequests_debuggerSee theUsagesection for more informationBut remember:Do not use it on production. I love this little hack but IT'S NOT NEEDED FOR PRODUCTIONFeaturesPrint out EVERY request you make using 'requests' libPrint usable strings for debugTraceback every requests callUsage:Singleton module:As Python's modules are singleton if you import 'requests_debbuger' and then import 'requests' it will not re-import 'requests' but just set the requests_debugger's 'request' module into your namespace. Therefore all your requests will have the debugger feature.>>>importrequests_debugger>>>importrequests>>>requests.get("http://test.com")/Users/nano/envs/bbb/lib/python2.7/site-packages/IPython/terminal/interactiveshell.pyLine:431/Users/nano/envs/bbb/lib/python2.7/site-packages/IPython/core/interactiveshell.pyLine:2881<ipython-input-3-f2de511e6e5e>Line:12017-02-2014:48:16-GET:http://test.com([],{}){}<Response[463]>Local moduleYou also can import just for that namespace or when you already have 'requests' imported>>>fromrequests_debuggerimportrequests>>>requests.get("http://test.com")/Users/nano/envs/bbb/lib/python2.7/site-packages/IPython/terminal/interactiveshell.pyLine:431/Users/nano/envs/bbb/lib/python2.7/site-packages/IPython/core/interactiveshell.pyLine:2881<ipython-input-2-f2de511e6e5e>Line:12017-02-2016:36:39-GET:http://test.com([],{}){}<Response[463]>Traceback featureLet's say that you have some models, APIs, libs, etc that internally uses 'requests' lib. You don't know when, where or why but it does. Just import the requests_debugger before anything else and it will traceback the request to you.>>>importrequests_debugger>>>fromexample.do_somethingimportwhatever>>>whatever()example/do_something.pyLine:8example/file_b.pyLine:8example/file_a.pyLine:102017-02-2015:01:03-GET:http://test.com([],{}){}But you may disable this, if you want:>>>importrequests_debugger>>>requests_debugger.set(max_depth=0)>>>whatever()2017-02-2015:03:51-GET:http://test.com([],{}){}Or make it deeper:>>>importrequests_debugger>>>requests_debugger.set(max_depth=10)>>>whatever()/Users/nano/envs/bbb/bin/ipythonLine:11/Users/nano/envs/bbb/lib/python2.7/site-packages/IPython/__init__.pyLine:119/Users/nano/envs/bbb/lib/python2.7/site-packages/traitlets/config/application.pyLine:658/Users/nano/envs/bbb/lib/python2.7/site-packages/IPython/terminal/ipapp.pyLine:348/Users/nano/envs/bbb/lib/python2.7/site-packages/IPython/terminal/interactiveshell.pyLine:431/Users/nano/envs/bbb/lib/python2.7/site-packages/IPython/core/interactiveshell.pyLine:2881<ipython-input-7-ad0274cf4d97>Line:1example/do_something.pyLine:8example/file_b.pyLine:8example/file_a.pyLine:102017-02-2015:05:09-GET:http://test.com([],{}){}Output formatsThe default output is 'LOG' format but you also have the useful python code output that you may just copy/paste to make that same request again.>>>importrequests>>>importrequests_debugger>>>requests_debugger.set(output_format=requests_debugger.PYTHON,max_depth=0)>>>fromexample.do_somethingimportwhatever>>>whatever()requests.get("http://test.com",)# Copy that output then just paste it back on python console>>>response=requests.get("http://test.com",)requests.get("http://test.com",)>>>response<Response[463]>Or cURL command that you may past on your terminal>>>importrequests_debugger>>>fromexample.do_somethingimportwhatever>>>requests_debugger.set(output_format=requests_debugger.CURL,max_depth=0)>>>whatever()curl-i-XGET'http://test.com'Then past on your terminal:$curl-i-XGET'http://test.com'HTTP/1.1302MovedTemporarily
Server:nginx/1.9.15
Date:Mon,20Feb201718:31:08GMT
Content-Type:text/html
Content-Length:161Connection:keep-alive
Keep-Alive:timeout=20Location:https://www.test.com/

<html>
<head><title>302Found</title></head>
<bodybgcolor="white">
<center><h1>302Found</h1></center>
<hr><center>nginx/1.9.15</center>
</body>
</html>Complex requestsMore complex requests are also translated to cURL ou python code>>>importrequests_debugger>>>importrequests>>>requests_debugger.set(output_format=requests_debugger.CURL,max_depth=0)>>>requests.post("http://test.com",data={"foo":"bar"},headers={"Authorization":"Basic IUYihda",'content-type':'application/json'},proxies={"http":"http://proxy.com"},cookies={"bar":"foo"})curl-i-XPOST--proxyhttp://http://proxy.com-H"content-type:application/json"-H"Authorization:Basic IUYihda"-H"Cookie:bar=foo"-d'{"foo": "bar"}''http://test.com'<Response[200]>>>>requests_debugger.set(output_format=requests_debugger.PYTHON)>>>requests.post("http://test.com",data={"foo":"bar"},headers={"Authorization":"Basic IUYihda",'content-type':'application/json'},proxies={"http":"http://proxy.com"},cookies={"bar":"foo"})requests.post("http://test.com",headers={'content-type':'application/json','Authorization':'Basic IUYihda'},cookies={'bar':'foo'},proxies={'http':'http://proxy.com'},data={'foo':'bar'})<Response[200]>Getting the standard 'requests' backuse unload() method>>>importrequests_debugger>>>importrequests>>>requests.get("http://test.com")/Users/nano/envs/bbb/lib/python2.7/site-packages/IPython/terminal/interactiveshell.pyLine:431/Users/nano/envs/bbb/lib/python2.7/site-packages/IPython/core/interactiveshell.pyLine:2881<ipython-input-3-f2de511e6e5e>Line:12017-02-2015:37:36-GET:http://test.com([],{}){}<Response[463]>>>>requests_debugger.unload()>>>requests.get("http://test.com")<Response[463]>TODO:Translate requests' 'auth' argument to cURL headers