From 35a0eecd79f3cbfbc0afcde6d925675fb64f944c Mon Sep 17 00:00:00 2001 From: Mauricio Baeza Date: Tue, 22 Jun 2021 22:54:25 -0500 Subject: [PATCH] Add examples for url open --- doc/source/main/easymacro.rst | 1 + doc/source/main/email.rst | 20 ++++++++++++++++++++ doc/source/main/tools.rst | 23 +++++++++++++++++++++++ source/easymacro.py | 2 +- 4 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 doc/source/main/email.rst diff --git a/doc/source/main/easymacro.rst b/doc/source/main/easymacro.rst index 9dc3803..b52c1a8 100644 --- a/doc/source/main/easymacro.rst +++ b/doc/source/main/easymacro.rst @@ -26,6 +26,7 @@ You can used **easymacro.py** with any extension or directly in your macros. tools_for_debug.rst tools.rst + email.rst application.rst calc.rst writer.rst diff --git a/doc/source/main/email.rst b/doc/source/main/email.rst new file mode 100644 index 0000000..b8d71fd --- /dev/null +++ b/doc/source/main/email.rst @@ -0,0 +1,20 @@ + +Email +----- + +Remember, always import library. + +.. code-block:: python + + import easymacro as app + + +Send email +^^^^^^^^^^ + +.. code-block:: python + + app.msgbox(app.OS) + + +.. _cryptography: https://github.com/pyca/cryptography diff --git a/doc/source/main/tools.rst b/doc/source/main/tools.rst index 9e5429e..beee2cb 100644 --- a/doc/source/main/tools.rst +++ b/doc/source/main/tools.rst @@ -530,6 +530,29 @@ You need install library `cryptography`_ return +Simple url open +^^^^^^^^^^^^^^^ + +* Get text data + +.. code-block:: python + + url = 'https://api.ipify.org' + data = app.url_open(url) + app.msgbox(data) + +* Get json data + +.. code-block:: python + + url = 'https://api.ipify.org?format=json' + data = app.url_open(url, get_json=True) + app.msgbox(data) + +For more complex case, you can used `requests`_ or `httpx`_ + .. _epoch time: https://en.wikipedia.org/wiki/Unix_time .. _cryptography: https://github.com/pyca/cryptography +.. _requests: https://docs.python-requests.org +.. _httpx: https://www.python-httpx.org/ diff --git a/source/easymacro.py b/source/easymacro.py index d6f9251..4faab64 100644 --- a/source/easymacro.py +++ b/source/easymacro.py @@ -781,7 +781,7 @@ def url_open(url, data=None, headers={}, verify=True, get_json=False): err = str(e.reason) else: headers = dict(response.info()) - result = response.read() + result = response.read().decode() if get_json: result = json.loads(result)