Main Content

Create Client Connection

The connection between a Python® client and a MATLAB® Production Server™ instance is encapsulated in a matlab.production_server.client.MWHttpClient object. You use the MWHttpClient constructor to instantiate the connection between the client and the server.

The MWHttpClient() constructor has the following signature:

client.MWHttpClient(url[,timeout_ms=timeout,ssl_context=ssl_context])

The constructor has the following arguments:

  • url — URL of the server instance to which the client connects. If the URL is to an on-premises server instance, the URL must contain the port number of the server instance.

    Note

    The URL contains only the host name and port information of the server instance.

  • timeout_ms — Amount of time, in milliseconds, that the client waits for a response before timing out.

    The default time-out interval is two minutes.

  • ssl_contextssl.SSLContext object that contains information about the SSL protocol to use for HTTPS communication with the server. If the URL of the server instance contains HTTPS, this argument is required.

    The default is to not use SSL.

Note

The MWHttpClient object is not thread-safe. If you are developing a multithreaded application, create a new MWHttpClient object for each thread.

Create Default Connection

To create a default connection, provide a value for the server instance URL. The timeout_ms argument has a default value, so you do not need to specify a time. The default is to use HTTP for client-server communication. This code sample shows how to connect to server instance on a host named mps_host using the default time-out of two minutes.

import matlab
from production_server import client 

my_client = client.MWHttpClient("http://mps_host:9910")

Configure Connection Timeout

You specify the connection time out by providing a value for the timeout_ms argument. This code sample specifies a time-out of one minute.

import matlab
from production_server import client 

my_client = client.MWHttpClient("http://mps_host:9910",timeout_ms=60000)

Use HTTPS for Client-Server Communication

The MATLAB Production Server Python client API uses the Python ssl library for supporting HTTPS communication with the server. You specify SSL connection properties by providing an object of the Python ssl.SSLContext class as value for the ssl_context argument. You can pass a parameter to the ssl.SSLContext object to set the SSL protocol to use. For more information about the SSL protocols that the server supports, see ssl-protocols.

HTTPS communication using the Python client API is supported only on Windows® and Linux® platforms. macOS is not supported.

This code sample sets the SSL protocol to PROTOCOL_TLS_CLIENT. Setting the protocol to PROTOCOL_TLS_CLIENT requires you to provide details about the SSL certificate of the server.

import ssl
import matlab
from production_server import client 

context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
context.load_verify_locations(cafile='<path_to_server_SSL_certificate_location>\cert_file.pem')
my_client = client.MWHttpClient("https://mps_host:9920",ssl_context=context)

See Also

Related Topics

External Websites