Add examples for url open
This commit is contained in:
parent
21fcf5f274
commit
35a0eecd79
|
@ -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
|
||||
|
|
|
@ -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
|
|
@ -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/
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
Loading…
Reference in New Issue