瀏覽代碼

Python examples; that's all; closes #50

Philipp Heckel 4 年之前
父節點
當前提交
4346f55b29
共有 3 個文件被更改,包括 30 次插入0 次删除
  1. 6 0
      docs/index.md
  2. 16 0
      docs/subscribe/api.md
  3. 8 0
      examples/subscribe-python/subscribe.py

+ 6 - 0
docs/index.md

@@ -52,6 +52,12 @@ is in the request body. Here's an example showing how to publish a simple messag
        strings.NewReader("Backup successful 😀"))
     ```
 
+=== "Python"
+    ``` python
+    requests.post("https://ntfy.sh/mytopic",
+        data="Backup successful 😀".encode(encoding='utf-8'))
+    ```
+
 === "PHP"
     ``` php-inline
     file_get_contents('https://ntfy.sh/mytopic', false, stream_context_create([

+ 16 - 0
docs/subscribe/api.md

@@ -54,6 +54,14 @@ recommended way to subscribe to a topic**. The notable exception is JavaScript,
     }
     ```
 
+=== "Python"
+    ``` python
+    resp = requests.get("https://ntfy.sh/disk-alerts/json", stream=True)
+    for line in resp.iter_lines():
+      if line:
+        print(line)
+    ```
+
 === "PHP"
     ``` php-inline
     $fp = fopen('https://ntfy.sh/disk-alerts/json', 'r');
@@ -150,6 +158,14 @@ format. Keepalive messages are sent as empty lines.
     }
     ```
 
+=== "Python"
+    ``` python 
+    resp = requests.get("https://ntfy.sh/disk-alerts/raw", stream=True)
+    for line in resp.iter_lines():
+      if line:
+        print(line)
+    ```
+
 === "PHP"
     ``` php-inline
     $fp = fopen('https://ntfy.sh/disk-alerts/raw', 'r');

+ 8 - 0
examples/subscribe-python/subscribe.py

@@ -0,0 +1,8 @@
+#!/usr/bin/env python3
+
+import requests
+
+resp = requests.get("https://ntfy.sh/mytopic/json", stream=True)
+for line in resp.iter_lines():
+    if line:
+        print(line)