msl.loadlib.server32 module
Contains the base class for loading a 32-bit shared library in 32-bit Python.
The Server32
class is used in combination with the
Client64
class to communicate with a 32-bit shared library
from 64-bit Python.
- class msl.loadlib.server32.Server32(path, libtype, host, port, **kwargs)
Bases:
HTTPServer
Base class for loading a 32-bit library in 32-bit Python.
All modules that are to be run on the 32-bit server must contain a class that is inherited from this class. The module may import most of the standard python modules.
All modules that are run on the 32-bit server must be able to run on the Python interpreter that the server is running on, see
version()
for how to determine the version of the Python interpreter.- Parameters:
path (str) – The path to the 32-bit library (see
LoadLibrary
)libtype (LibType) –
The library type (see
LoadLibrary
).host (str) – The IP address (or hostname) to use for the server.
port (int) – The port to open for the server.
kwargs (Any) – All keyword arguments are passed to
LoadLibrary
.
- property assembly
Returns a reference to the .NET Runtime Assembly object if the shared library is .NET, otherwise returns
None
.Tip
The JetBrains dotPeek program can be used to reliably decompile any .NET Assembly into the equivalent source code.
- property lib
Returns the reference to the library object.
For example, if libtype is
- static version()
Returns the version of Python that the 32-bit server is running on.
Note
This method takes about 1 second to finish because the 32-bit server needs to start in order to determine the version of the Python interpreter.
- Return type:
- static interactive_console()
Start an interactive console.
This method starts an interactive console, in a new terminal, with the Python interpreter on the 32-bit server.
- Return type:
None
- static remove_site_packages_64bit()
Remove the site-packages directory from the 64-bit process.
By default, the site-packages directory of the 64-bit process is included in
sys.path
of the 32-bit process. Having the 64-bit site-packages directory available can sometimes cause issues. For example, comtypes imports numpy so if numpy is installed in the 64-bit process then comtypes will import the 64-bit version of numpy in the 32-bit process. Depending on the version of Python and/or numpy this can cause the 32-bit server to crash.New in version 0.9.
Example:
import sys from msl.loadlib import Server32 class FileSystem(Server32): def __init__(self, host, port, **kwargs): # remove the site-packages directory that was passed from 64-bit Python # before calling the super() function to load the COM library path = Server32.remove_site_packages_64bit() super().__init__('Scripting.FileSystemObject', 'com', host, port) # optional: add the site-packages directory back into sys.path sys.path.append(path)
- static is_interpreter()
Check if code is running on the 32-bit server.
If the same module is executed by both
Client64
andServer32
then there may be only parts of the code that should only be executed by the correct bitness of the Python interpreter.New in version 0.9.
Example:
import sys from msl.loadlib import Server32 if Server32.is_interpreter(): # this only gets executed on the 32-bit server assert sys.maxsize < 2**32
- Returns:
Whether the code is running on the 32-bit server.
- Return type:
- static examples_dir()
Returns the directory where the example libraries are located.
New in version 0.9.
- Return type:
- shutdown_handler()
Proxy function that is called immediately prior to the server shutting down.
The intended use case is for the server to do any necessary cleanup, such as stopping locally started threads or closing file handles before it shuts down.
New in version 0.6.
- Return type:
None