From 59e566117b9f091198924adb9cc8b6fc4ad405e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C4=93teris=20Caune?= Date: Fri, 17 Jul 2020 17:21:10 +0300 Subject: [PATCH] Update pinging examples. --- templates/docs/csharp.html | 2 +- templates/docs/csharp.md | 2 +- templates/docs/python.html | 17 ++++++++--------- templates/docs/python.md | 17 ++++++++--------- templates/front/snippets/go.html | 13 +++++++++---- templates/front/snippets/go.txt | 13 +++++++++---- templates/front/snippets/python_requests.html | 2 +- templates/front/snippets/python_requests.txt | 4 ++-- templates/front/snippets/python_urllib2.html | 12 +++++++----- templates/front/snippets/python_urllib2.txt | 12 +++++++----- 10 files changed, 53 insertions(+), 41 deletions(-) diff --git a/templates/docs/csharp.html b/templates/docs/csharp.html index 270612a2..7982903c 100644 --- a/templates/docs/csharp.html +++ b/templates/docs/csharp.html @@ -1,4 +1,4 @@ -

C

+

C#

Below is an example of making a HTTP request to SITE_NAME from C#.

using (var client = new System.Net.WebClient())
 {
diff --git a/templates/docs/csharp.md b/templates/docs/csharp.md
index af60190e..2a18c57f 100644
--- a/templates/docs/csharp.md
+++ b/templates/docs/csharp.md
@@ -1,4 +1,4 @@
-# C#
+# C\#
 
 Below is an example of making a HTTP request to SITE_NAME from C#.
 
diff --git a/templates/docs/python.html b/templates/docs/python.html
index 7042121e..65e37376 100644
--- a/templates/docs/python.html
+++ b/templates/docs/python.html
@@ -1,5 +1,5 @@
 

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:

import requests
 
 try:
@@ -10,16 +10,15 @@
 
-

Otherwise, you can use the urllib standard module.

-
# urllib with python 3.x:
+

Otherwise, you can use the urllib module from Python 3 standard libary:

+
import socket
 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)
 
diff --git a/templates/docs/python.md b/templates/docs/python.md index 361e7900..4c1484e4 100644 --- a/templates/docs/python.md +++ b/templates/docs/python.md @@ -1,6 +1,6 @@ # 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 import requests @@ -12,18 +12,17 @@ except requests.RequestException as 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 -# urllib with python 3.x: +import socket 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): diff --git a/templates/front/snippets/go.html b/templates/front/snippets/go.html index b0d4ae51..07397def 100644 --- a/templates/front/snippets/go.html +++ b/templates/front/snippets/go.html @@ -2,11 +2,16 @@ import "fmt" import "net/http" +import "time" 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) + } }
diff --git a/templates/front/snippets/go.txt b/templates/front/snippets/go.txt index e3213a48..06cc6a5b 100644 --- a/templates/front/snippets/go.txt +++ b/templates/front/snippets/go.txt @@ -2,10 +2,15 @@ package main import "fmt" import "net/http" +import "time" 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) + } } diff --git a/templates/front/snippets/python_requests.html b/templates/front/snippets/python_requests.html index 70a90711..e0742030 100644 --- a/templates/front/snippets/python_requests.html +++ b/templates/front/snippets/python_requests.html @@ -1,4 +1,4 @@ -
# using requests:
+
# Using the requests library:
 import requests
 
 try:
diff --git a/templates/front/snippets/python_requests.txt b/templates/front/snippets/python_requests.txt
index 31073f28..0e6f9a1c 100644
--- a/templates/front/snippets/python_requests.txt
+++ b/templates/front/snippets/python_requests.txt
@@ -1,8 +1,8 @@
-# using requests:
+# Using the requests library:
 import requests
 
 try:
     requests.get("PING_URL", timeout=10)
 except requests.RequestException as e:
     # Log ping failure here...
-    print("Ping failed: %s" % e)
\ No newline at end of file
+    print("Ping failed: %s" % e)
diff --git a/templates/front/snippets/python_urllib2.html b/templates/front/snippets/python_urllib2.html
index 6cfcaae2..8a6562fe 100644
--- a/templates/front/snippets/python_urllib2.html
+++ b/templates/front/snippets/python_urllib2.html
@@ -1,8 +1,10 @@
-
# urllib with python 3.x:
+
# Using Python 3 standard library:
+import socket
 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)
 
diff --git a/templates/front/snippets/python_urllib2.txt b/templates/front/snippets/python_urllib2.txt index e4b89638..c72e5694 100644 --- a/templates/front/snippets/python_urllib2.txt +++ b/templates/front/snippets/python_urllib2.txt @@ -1,7 +1,9 @@ -# urllib with python 3.x: +# Using Python 3 standard library: +import socket 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) \ No newline at end of file