.. 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: Choose configuration at runtime ------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 30-31 Import the necessary modules. .. GENERATED FROM PYTHON SOURCE LINES 31-38 .. code-block:: Python import os from example_httpserver_plugin import LauncherConfig from ansys.tools.local_product_launcher import launch_product .. GENERATED FROM PYTHON SOURCE LINES 39-44 Default configuration ~~~~~~~~~~~~~~~~~~~~~ First, launch the product without any configuration. This falls back to the default configuration. .. GENERATED FROM PYTHON SOURCE LINES 44-48 .. 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:45397'} .. GENERATED FROM PYTHON SOURCE LINES 49-50 To ensure that the server is running, use the ``wait()`` method. .. GENERATED FROM PYTHON SOURCE LINES 50-52 .. code-block:: Python product_instance.wait(timeout=5) .. GENERATED FROM PYTHON SOURCE LINES 53-55 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 55-61 .. 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 62-68 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 68-78 .. 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:44023'} .. GENERATED FROM PYTHON SOURCE LINES 79-80 Again, ensure that the server is running. .. GENERATED FROM PYTHON SOURCE LINES 80-82 .. code-block:: Python product_instance.wait(timeout=5) .. GENERATED FROM PYTHON SOURCE LINES 83-84 Get the content of the main page. .. GENERATED FROM PYTHON SOURCE LINES 84-89 .. 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 90-91 You can see that the server is now showing the files from the parent directory. .. GENERATED FROM PYTHON SOURCE LINES 93-99 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 99-102 .. code-block:: Python product_instance.stop() .. GENERATED FROM PYTHON SOURCE LINES 103-104 To ensure that the server is down, try to access the main page again. .. GENERATED FROM PYTHON SOURCE LINES 104-109 .. 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.347 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 ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_