Browse Source

Update pinging examples.

pull/405/head
Pēteris Caune 4 years ago
parent
commit
59e566117b
No known key found for this signature in database GPG Key ID: E28D7679E9A9EDE2
10 changed files with 53 additions and 41 deletions
  1. +1
    -1
      templates/docs/csharp.html
  2. +1
    -1
      templates/docs/csharp.md
  3. +8
    -9
      templates/docs/python.html
  4. +8
    -9
      templates/docs/python.md
  5. +9
    -4
      templates/front/snippets/go.html
  6. +9
    -4
      templates/front/snippets/go.txt
  7. +1
    -1
      templates/front/snippets/python_requests.html
  8. +2
    -2
      templates/front/snippets/python_requests.txt
  9. +7
    -5
      templates/front/snippets/python_urllib2.html
  10. +7
    -5
      templates/front/snippets/python_urllib2.txt

+ 1
- 1
templates/docs/csharp.html View File

@ -1,4 +1,4 @@
<h1>C</h1>
<h1>C#</h1>
<p>Below is an example of making a HTTP request to SITE_NAME from C#.</p> <p>Below is an example of making a HTTP request to SITE_NAME from C#.</p>
<div class="highlight"><pre><span></span><code><span class="k">using</span> <span class="p">(</span><span class="kt">var</span> <span class="n">client</span> <span class="p">=</span> <span class="k">new</span> <span class="n">System</span><span class="p">.</span><span class="n">Net</span><span class="p">.</span><span class="n">WebClient</span><span class="p">())</span> <div class="highlight"><pre><span></span><code><span class="k">using</span> <span class="p">(</span><span class="kt">var</span> <span class="n">client</span> <span class="p">=</span> <span class="k">new</span> <span class="n">System</span><span class="p">.</span><span class="n">Net</span><span class="p">.</span><span class="n">WebClient</span><span class="p">())</span>
<span class="p">{</span> <span class="p">{</span>


+ 1
- 1
templates/docs/csharp.md View File

@ -1,4 +1,4 @@
# C#
# C\#
Below is an example of making a HTTP request to SITE_NAME from C#. Below is an example of making a HTTP request to SITE_NAME from C#.


+ 8
- 9
templates/docs/python.html View File

@ -1,5 +1,5 @@
<h1>Python</h1> <h1>Python</h1>
<p>If you are already using the requests library, it's convenient to also use it here:</p>
<p>If you are already using the requests library, it is convenient to also use it here:</p>
<div class="highlight"><pre><span></span><code><span class="kn">import</span> <span class="nn">requests</span> <div class="highlight"><pre><span></span><code><span class="kn">import</span> <span class="nn">requests</span>
<span class="k">try</span><span class="p">:</span> <span class="k">try</span><span class="p">:</span>
@ -10,16 +10,15 @@
</code></pre></div> </code></pre></div>
<p>Otherwise, you can use the urllib standard module.</p>
<div class="highlight"><pre><span></span><code><span class="c1"># urllib with python 3.x:</span>
<p>Otherwise, you can use the urllib module from Python 3 standard libary:</p>
<div class="highlight"><pre><span></span><code><span class="kn">import</span> <span class="nn">socket</span>
<span class="kn">import</span> <span class="nn">urllib.request</span> <span class="kn">import</span> <span class="nn">urllib.request</span>
<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>
</code></pre></div>
<div class="highlight"><pre><span></span><code><span class="c1"># urllib with python 2.x:</span>
<span class="kn">import</span> <span class="nn">urllib</span>
<span class="n">urllib</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="k">try</span><span class="p">:</span>
<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>
<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>
<span class="c1"># Log ping failure here...</span>
<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>
</code></pre></div> </code></pre></div>


+ 8
- 9
templates/docs/python.md View File

@ -1,6 +1,6 @@
# Python # Python
If you are already using the requests library, it's convenient to also use it here:
If you are already using the requests library, it is convenient to also use it here:
```python ```python
import requests import requests
@ -12,18 +12,17 @@ except requests.RequestException as e:
print("Ping failed: %s" % e) print("Ping failed: %s" % e)
``` ```
Otherwise, you can use the urllib standard module.
Otherwise, you can use the urllib module from Python 3 standard libary:
```python ```python
# urllib with python 3.x:
import socket
import urllib.request import urllib.request
urllib.request.urlopen("PING_URL")
```
```python
# urllib with python 2.x:
import urllib
urllib.urlopen("PING_URL")
try:
urllib.request.urlopen("PING_URL", timeout=10)
except socket.error as e:
# Log ping failure here...
print("Ping failed: %s" % e)
``` ```
You can include additional diagnostic information in the in the request body (for POST requests): You can include additional diagnostic information in the in the request body (for POST requests):


+ 9
- 4
templates/front/snippets/go.html View File

@ -2,11 +2,16 @@
<span class="kn">import</span> <span class="s">&quot;fmt&quot;</span> <span class="kn">import</span> <span class="s">&quot;fmt&quot;</span>
<span class="kn">import</span> <span class="s">&quot;net/http&quot;</span> <span class="kn">import</span> <span class="s">&quot;net/http&quot;</span>
<span class="kn">import</span> <span class="s">&quot;time&quot;</span>
<span class="kd">func</span> <span class="nx">main</span><span class="p">()</span> <span class="p">{</span> <span class="kd">func</span> <span class="nx">main</span><span class="p">()</span> <span class="p">{</span>
<span class="nx">_</span><span class="p">,</span> <span class="nx">err</span> <span class="o">:=</span> <span class="nx">http</span><span class="p">.</span><span class="nx">Head</span><span class="p">(</span><span class="s">&quot;{{ ping_url }}&quot;</span><span class="p">)</span>
<span class="k">if</span> <span class="nx">err</span> <span class="o">!=</span> <span class="kc">nil</span> <span class="p">{</span>
<span class="nx">fmt</span><span class="p">.</span><span class="nx">Printf</span><span class="p">(</span><span class="s">&quot;%s&quot;</span><span class="p">,</span> <span class="nx">err</span><span class="p">)</span>
<span class="p">}</span>
<span class="kd">var</span> <span class="nx">client</span> <span class="p">=</span> <span class="o">&amp;</span><span class="nx">http</span><span class="p">.</span><span class="nx">Client</span><span class="p">{</span>
<span class="nx">Timeout</span><span class="p">:</span> <span class="mi">10</span> <span class="o">*</span> <span class="nx">time</span><span class="p">.</span><span class="nx">Second</span><span class="p">,</span>
<span class="p">}</span>
<span class="nx">_</span><span class="p">,</span> <span class="nx">err</span> <span class="o">:=</span> <span class="nx">client</span><span class="p">.</span><span class="nx">Head</span><span class="p">(</span><span class="s">&quot;{{ ping_url }}&quot;</span><span class="p">)</span>
<span class="k">if</span> <span class="nx">err</span> <span class="o">!=</span> <span class="kc">nil</span> <span class="p">{</span>
<span class="nx">fmt</span><span class="p">.</span><span class="nx">Printf</span><span class="p">(</span><span class="s">&quot;%s&quot;</span><span class="p">,</span> <span class="nx">err</span><span class="p">)</span>
<span class="p">}</span>
<span class="p">}</span> <span class="p">}</span>
</pre></div> </pre></div>

+ 9
- 4
templates/front/snippets/go.txt View File

@ -2,10 +2,15 @@ package main
import "fmt" import "fmt"
import "net/http" import "net/http"
import "time"
func main() { func main() {
_, err := http.Head("PING_URL")
if err != nil {
fmt.Printf("%s", err)
}
var client = &http.Client{
Timeout: 10 * time.Second,
}
_, err := client.Head("PING_URL")
if err != nil {
fmt.Printf("%s", err)
}
} }

+ 1
- 1
templates/front/snippets/python_requests.html View File

@ -1,4 +1,4 @@
<div class="highlight"><pre><span></span><span class="c1"># using requests:</span>
<div class="highlight"><pre><span></span><span class="c1"># Using the requests library:</span>
<span class="kn">import</span> <span class="nn">requests</span> <span class="kn">import</span> <span class="nn">requests</span>
<span class="k">try</span><span class="p">:</span> <span class="k">try</span><span class="p">:</span>


+ 2
- 2
templates/front/snippets/python_requests.txt View File

@ -1,8 +1,8 @@
# using requests:
# Using the requests library:
import requests import requests
try: try:
requests.get("PING_URL", timeout=10) requests.get("PING_URL", timeout=10)
except requests.RequestException as e: except requests.RequestException as e:
# Log ping failure here... # Log ping failure here...
print("Ping failed: %s" % e)
print("Ping failed: %s" % e)

+ 7
- 5
templates/front/snippets/python_urllib2.html View File

@ -1,8 +1,10 @@
<div class="highlight"><pre><span></span><span class="c1"># urllib with python 3.x:</span>
<div class="highlight"><pre><span></span><span class="c1"># Using Python 3 standard library:</span>
<span class="kn">import</span> <span class="nn">socket</span>
<span class="kn">import</span> <span class="nn">urllib.request</span> <span class="kn">import</span> <span class="nn">urllib.request</span>
<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="c1"># urllib with python 2.x:</span>
<span class="kn">import</span> <span class="nn">urllib</span>
<span class="n">urllib</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="k">try</span><span class="p">:</span>
<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>
<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>
<span class="c1"># Log ping failure here...</span>
<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>
</pre></div> </pre></div>

+ 7
- 5
templates/front/snippets/python_urllib2.txt View File

@ -1,7 +1,9 @@
# urllib with python 3.x:
# Using Python 3 standard library:
import socket
import urllib.request import urllib.request
urllib.request.urlopen("PING_URL")
# urllib with python 2.x:
import urllib
urllib.urlopen("PING_URL")
try:
urllib.request.urlopen("PING_URL", timeout=10)
except socket.error as e:
# Log ping failure here...
print("Ping failed: %s" % e)

Loading…
Cancel
Save