.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "gyexamples/plot_rest_api.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_gyexamples_plot_rest_api.py: Starts and queries a REST API ============================= This example starts a :epkg:`waitress` server, creates a :epkg:`WSGI` application based on :epkg:`falcon` and queries the REST API. This application returns the prediction from a model trained on :epkg:`Iris dataset`. .. GENERATED FROM PYTHON SOURCE LINES 12-13 Settings. .. GENERATED FROM PYTHON SOURCE LINES 13-16 .. code-block:: default host = '127.0.0.1' port = 8096 .. GENERATED FROM PYTHON SOURCE LINES 17-19 Creates a dummy application and starts a server in a different process. See :func:`dummy_application `. .. GENERATED FROM PYTHON SOURCE LINES 19-28 .. code-block:: default def process_server(host, port): from lightmlrestapi.testing import dummy_application app = dummy_application() from waitress import serve serve(app, host=host, port=port) .. GENERATED FROM PYTHON SOURCE LINES 29-31 Saves this code into a file and we start it from a different process. .. GENERATED FROM PYTHON SOURCE LINES 31-64 .. code-block:: default import os import lightmlrestapi header = """ import sys sys.path.append(r'{0}') """.format(os.path.join(os.path.dirname(lightmlrestapi.__file__), '..')) import inspect code = "".join(inspect.getsourcelines(process_server)[0]) code = header + code + "\nprocess_server('{0}', {1})\n".format(host, port) dest = os.path.abspath('temp_scripts') if not os.path.exists(dest): os.mkdir(dest) code_file = os.path.join(dest, "_start_server.py") print("Write file '{0}'.".format(code_file)) with open(code_file, "w") as f: f.write(code) import sys from subprocess import Popen if sys.platform.startswith('win'): cmd = '{0} -u "{1}"'.format(sys.executable, code_file) print("Running '{0}'".format(cmd)) proc = Popen(cmd) else: cmd = [sys.executable, '-u', code_file] print("Running '{0}'".format(cmd)) proc = Popen(cmd) print('Start server, process id', proc.pid) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none Write file 'somewhere/workspace/lightmlrestapi/lightmlrestapi_UT_39_std/_doc/examples/temp_scripts/_start_server.py'. Running '['somewhere/workspace/lightmlrestapi/lightmlrestapi_UT_39_std/_venv/bin/python3.9', '-u', 'somewhere/workspace/lightmlrestapi/lightmlrestapi_UT_39_std/_doc/examples/temp_scripts/_start_server.py']' Start server, process id 32383 .. GENERATED FROM PYTHON SOURCE LINES 65-66 Let's wait. .. GENERATED FROM PYTHON SOURCE LINES 66-69 .. code-block:: default from time import sleep sleep(3) .. GENERATED FROM PYTHON SOURCE LINES 70-71 Let's query the server. .. GENERATED FROM PYTHON SOURCE LINES 71-81 .. code-block:: default import requests import ujson features = ujson.dumps({'X': [0.1, 0.2]}) try: r = requests.post('http://%s:%d' % (host, port), data=features) print(r.json()) except Exception as e: print("ERROR: %r" % e) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none ERROR: ConnectionError(MaxRetryError("HTTPConnectionPool(host='127.0.0.1', port=8096): Max retries exceeded with url: / (Caused by NewConnectionError(': Failed to establish a new connection: [Errno 111] Connection refused'))")) .. GENERATED FROM PYTHON SOURCE LINES 82-83 Let's stop the server. .. GENERATED FROM PYTHON SOURCE LINES 83-85 .. code-block:: default from pyquickhelper.loghelper import reap_children reap_children(subset={proc.pid}, fLOG=print) .. rst-class:: sphx-glr-script-out Out: .. code-block:: none process psutil.Process(pid=32383, status='terminated', exitcode=, started='07:17:18') terminated with exit code -15 {32383} .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 3.083 seconds) .. _sphx_glr_download_gyexamples_plot_rest_api.py: .. only :: html .. container:: sphx-glr-footer :class: sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_rest_api.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_rest_api.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_