.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/py_configure.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_examples_py_configure.py: .. _ref_py_configure: Choose configuration at runtime ------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 32-33 Import the necessary modules. .. GENERATED FROM PYTHON SOURCE LINES 33-40 .. code-block:: Python import os from example_httpserver_plugin import LauncherConfig from ansys.tools.common.launcher import launch_product .. GENERATED FROM PYTHON SOURCE LINES 41-46 Default configuration ~~~~~~~~~~~~~~~~~~~~~ First, launch the product without any configuration. This falls back to the default configuration. .. GENERATED FROM PYTHON SOURCE LINES 46-50 .. code-block:: Python product_instance = launch_product(product_name="example_httpserver") product_instance.urls .. rst-class:: sphx-glr-script-out .. code-block:: none {'main': 'localhost:44865'} .. GENERATED FROM PYTHON SOURCE LINES 51-52 To ensure that the server is running, use the ``wait()`` method. .. GENERATED FROM PYTHON SOURCE LINES 52-54 .. code-block:: Python product_instance.wait(timeout=5) .. GENERATED FROM PYTHON SOURCE LINES 55-57 Retrieve the content of the server's main page. This simply serves a list of files in the directory where the server was launched. .. GENERATED FROM PYTHON SOURCE LINES 57-63 .. code-block:: Python import requests res = requests.get(f"http://{product_instance.urls['main']}") print(res.content.decode("utf-8")) .. rst-class:: sphx-glr-script-out .. code-block:: none Directory listing for /

Directory listing for /



.. GENERATED FROM PYTHON SOURCE LINES 64-70 Custom configuration ~~~~~~~~~~~~~~~~~~~~ Now, try to launch the product with a custom configuration. This is done by passing the ``config`` and ``launch_mode`` arguments to the :func:`.launch_product` function. .. GENERATED FROM PYTHON SOURCE LINES 70-80 .. code-block:: Python directory = os.path.join(os.getcwd(), "..") product_instance = launch_product( product_name="example_httpserver", config=LauncherConfig(directory=directory), launch_mode="direct", ) product_instance.urls .. rst-class:: sphx-glr-script-out .. code-block:: none {'main': 'localhost:36163'} .. GENERATED FROM PYTHON SOURCE LINES 81-82 Again, ensure that the server is running. .. GENERATED FROM PYTHON SOURCE LINES 82-84 .. code-block:: Python product_instance.wait(timeout=5) .. GENERATED FROM PYTHON SOURCE LINES 85-86 Get the content of the main page. .. GENERATED FROM PYTHON SOURCE LINES 86-91 .. code-block:: Python full_url = f"http://{product_instance.urls['main']}" res = requests.get(full_url) print(res.content.decode("utf-8")) .. rst-class:: sphx-glr-script-out .. code-block:: none Directory listing for /

Directory listing for /



.. GENERATED FROM PYTHON SOURCE LINES 92-93 You can see that the server is now showing the files from the parent directory. .. GENERATED FROM PYTHON SOURCE LINES 95-101 Teardown ~~~~~~~~ You can manually stop the server using the :meth:`stop() <.ProductInstance.stop>` method. Alternatively, the server is stopped when all references to ``product_instance`` are deleted. .. GENERATED FROM PYTHON SOURCE LINES 101-104 .. code-block:: Python product_instance.stop() .. GENERATED FROM PYTHON SOURCE LINES 105-106 To ensure that the server is down, try to access the main page again. .. GENERATED FROM PYTHON SOURCE LINES 106-111 .. code-block:: Python try: requests.get(full_url) except requests.ConnectionError: print("The server is down.") .. rst-class:: sphx-glr-script-out .. code-block:: none The server is down. .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.894 seconds) .. _sphx_glr_download_examples_py_configure.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: py_configure.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: py_configure.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: py_configure.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_