You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

28 lines
2.9 KiB

4 years ago
4 years ago
4 years ago
  1. <h1>Python</h1>
  2. <p>If you are already using the <a href="https://requests.readthedocs.io/en/master/">requests</a>
  3. library, it is convenient to also use it here:</p>
  4. <div class="highlight"><pre><span></span><code><span class="kn">import</span> <span class="nn">requests</span>
  5. <span class="k">try</span><span class="p">:</span>
  6. <span class="n">requests</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s2">&quot;PING_URL&quot;</span><span class="p">,</span> <span class="n">timeout</span><span class="o">=</span><span class="mi">10</span><span class="p">)</span>
  7. <span class="k">except</span> <span class="n">requests</span><span class="o">.</span><span class="n">RequestException</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
  8. <span class="c1"># Log ping failure here...</span>
  9. <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;Ping failed: </span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="n">e</span><span class="p">)</span>
  10. </code></pre></div>
  11. <p>Otherwise, you can use the <a href="https://docs.python.org/3/library/urllib.request.html">urllib.request</a>
  12. module from Python 3 standard library:</p>
  13. <div class="highlight"><pre><span></span><code><span class="kn">import</span> <span class="nn">socket</span>
  14. <span class="kn">import</span> <span class="nn">urllib.request</span>
  15. <span class="k">try</span><span class="p">:</span>
  16. <span class="n">urllib</span><span class="o">.</span><span class="n">request</span><span class="o">.</span><span class="n">urlopen</span><span class="p">(</span><span class="s2">&quot;PING_URL&quot;</span><span class="p">,</span> <span class="n">timeout</span><span class="o">=</span><span class="mi">10</span><span class="p">)</span>
  17. <span class="k">except</span> <span class="n">socket</span><span class="o">.</span><span class="n">error</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
  18. <span class="c1"># Log ping failure here...</span>
  19. <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;Ping failed: </span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="n">e</span><span class="p">)</span>
  20. </code></pre></div>
  21. <p>You can include additional diagnostic information in the in the request body (for POST requests):</p>
  22. <div class="highlight"><pre><span></span><code><span class="c1"># Passing diagnostic information in the POST body:</span>
  23. <span class="kn">import</span> <span class="nn">requests</span>
  24. <span class="n">requests</span><span class="o">.</span><span class="n">post</span><span class="p">(</span><span class="s2">&quot;PING_URL&quot;</span><span class="p">,</span> <span class="n">data</span><span class="o">=</span><span class="s2">&quot;temperature=-7&quot;</span><span class="p">)</span>
  25. </code></pre></div>