https://freegeoip.net/; python sample below.
<span class="kwd">import</span><span class="pln"> requests FREEGEOPIP_URL </span><span class="pun">=</span> <span class="str">'http://freegeoip.net/json/'</span><span class="pln"> SAMPLE_RESPONSE </span><span class="pun">=</span> <span class="str">"""{ "ip":"108.46.131.77", "country_code":"US", "country_name":"United States", "region_code":"NY", "region_name":"New York", "city":"Brooklyn", "zip_code":"11249", "time_zone":"America/New_York", "latitude":40.645, "longitude":-73.945, "metro_code":501 }"""</span> <span class="kwd">def</span><span class="pln"> get_geolocation_for_ip</span><span class="pun">(</span><span class="pln">ip</span><span class="pun">):</span><span class="pln"> url </span><span class="pun">=</span> <span class="str">'{}/{}'</span><span class="pun">.</span><span class="pln">format</span><span class="pun">(</span><span class="pln">FREEGEOPIP_URL</span><span class="pun">,</span><span class="pln"> ip</span><span class="pun">)</span><span class="pln"> response </span><span class="pun">=</span><span class="pln"> requests</span><span class="pun">.</span><span class="pln">get</span><span class="pun">(</span><span class="pln">url</span><span class="pun">)</span><span class="pln"> response</span><span class="pun">.</span><span class="pln">raise_for_status</span><span class="pun">()</span> <span class="kwd">return</span><span class="pln"> response</span><span class="pun">.</span><span class="pln">json</span><span class="pun">(</span>
Profiling Python web applications with visual tools
I have been profiling my software with KCachegrind for a long time now and I was missing this option when I am developing API’s or other web services. I always knew that this is possible but never really took the time and dive into it.
Before we begin there are some requirements. We will need to:
- implement cProfile into our web app,
- convert output to callgrind format with pyprof2calltree,
- visualize data with KCachegrind or Profiling Viewer.
Raymond Hettinger, “Being a Core Developer in Python”, PyBay2016 SF Python
PyBay 2016 Keynote
Don’t trample other people’s flower gardens.
Don’t rearrange Helen Keller’s furniture.
Asynchronous HTTP Requests in Python
So 22 seconds versus 12 minutes is a very huge difference in performance and it should be noted that the outcomes of the 2 programs are exactly the same.