### Get Raw Machine GUID with py-machineid Source: https://context7.com/keygen-sh/py-machineid/llms.txt Retrieve the platform-specific device GUID as a plain string. Optionally disable the Windows registry query for enhanced security. ```python import machineid # Basic usage — get the raw machine GUID try: machine_id = machineid.id() print(machine_id) # Example output: "17A28A73-BEA9-4D4B-AF5B-03A5AAE9B92C" except machineid.MachineIdNotFound as e: print(f"Could not determine machine ID: {e}") # On Windows: disable registry query for tighter security posture # (falls back to WMI/CIM PowerShell call) try: machine_id = machineid.id(winregistry=False) print(machine_id) except machineid.MachineIdNotFound as e: print(f"Could not determine machine ID: {e}") ``` -------------------------------- ### Install py-machineid Source: https://github.com/keygen-sh/py-machineid/blob/master/README.md Install the py-machineid package using pip. This command ensures you have the latest version available. ```bash python3 -m pip install py-machineid ``` -------------------------------- ### machineid.id() — Get Raw Machine GUID Source: https://context7.com/keygen-sh/py-machineid/llms.txt Retrieves the platform-specific device GUID as a plain string. Optionally controls Windows registry querying and raises MachineIdNotFound if the ID cannot be determined. ```APIDOC ## machineid.id() ### Description Returns the platform-specific device GUID as a plain string. Accepts an optional `winregistry: bool` keyword argument (default `True`) to control whether the Windows registry is queried. Raises `MachineIdNotFound` if the ID cannot be determined on the current platform. ### Method `machineid.id(winregistry: bool = True)` ### Parameters #### Keyword Arguments - **winregistry** (bool) - Optional - Default `True`. If `True`, queries the Windows registry for the machine GUID. If `False`, falls back to other methods. ### Request Example ```python import machineid try: machine_id = machineid.id() print(machine_id) except machineid.MachineIdNotFound as e: print(f"Could not determine machine ID: {e}") try: machine_id = machineid.id(winregistry=False) print(machine_id) except machineid.MachineIdNotFound as e: print(f"Could not determine machine ID: {e}") ``` ### Response #### Success Response - **machine_id** (str) - The raw machine GUID string. #### Response Example ``` "17A28A73-BEA9-4D4B-AF5B-03A5AAE9B92C" ``` #### Error Response - **MachineIdNotFound** - Raised when the machine ID cannot be determined. ``` -------------------------------- ### Get Raw Machine ID Source: https://github.com/keygen-sh/py-machineid/blob/master/README.md Use the `id()` function to retrieve the raw GUID of the device. This function requires no arguments. ```python import machineid print(machineid.id()) ``` -------------------------------- ### Get Anonymized Machine ID with py-machineid Source: https://context7.com/keygen-sh/py-machineid/llms.txt Obtain an HMAC-SHA256 hex digest of the machine ID, optionally scoped to an application ID. This prevents raw GUID exposure and reuse across applications. The Windows registry query can also be disabled. ```python import machineid # Anonymized ID scoped to a specific application try: app_machine_id = machineid.hashed_id('myappid') print(app_machine_id) # Example output: "366048092ef4e7db53cd7adec82dcab15ab67ac2a6b234dc6a69303a4dd48e83" except machineid.MachineIdNotFound as e: print(f"Could not determine machine ID: {e}") # Anonymized ID with no app_id (empty string key) try: generic_hashed = machineid.hashed_id() print(generic_hashed) # Example output: "ce2127ade536eaa9529f4a7b73141bbc2f094c46e32742c97679e186e7f13fde" except machineid.MachineIdNotFound as e: print(f"Could not determine machine ID: {e}") # On Windows: anonymized ID without registry query try: secure_id = machineid.hashed_id('myappid', winregistry=False) print(secure_id) except machineid.MachineIdNotFound as e: print(f"Could not determine machine ID: {e}") ``` -------------------------------- ### Get Hashed Machine ID Source: https://github.com/keygen-sh/py-machineid/blob/master/README.md Use the `hashed_id()` function to obtain an anonymized (hashed) version of the machine ID. An optional application ID can be provided to ensure a unique ID per-app for the same device. ```python import machineid print(machineid.hashed_id('myappid')) print(machineid.hashed_id()) ``` -------------------------------- ### machineid.hashed_id() — Get Anonymized Machine ID Source: https://context7.com/keygen-sh/py-machineid/llms.txt Returns an HMAC-SHA256 hex digest of the machine ID. An optional `app_id` argument keys the HMAC, producing a unique fingerprint per app. Accepts the same `winregistry` kwarg as `id()`. ```APIDOC ## machineid.hashed_id() ### Description Returns an HMAC-SHA256 hex digest of the machine ID. An optional `app_id: str` argument keys the HMAC with the application ID, producing a unique fingerprint per app for the same device. This prevents raw GUIDs from being exposed or reused across applications. Accepts the same `winregistry` kwarg as `id()`. ### Method `machineid.hashed_id(app_id: str = '', winregistry: bool = True)` ### Parameters #### Arguments - **app_id** (str) - Optional - Default `''`. An application identifier to scope the HMAC. If empty, a generic hash is produced. - **winregistry** (bool) - Optional - Default `True`. If `True`, queries the Windows registry for the machine GUID. If `False`, falls back to other methods. ### Request Example ```python import machineid try: app_machine_id = machineid.hashed_id('myappid') print(app_machine_id) except machineid.MachineIdNotFound as e: print(f"Could not determine machine ID: {e}") try: generic_hashed = machineid.hashed_id() print(generic_hashed) except machineid.MachineIdNotFound as e: print(f"Could not determine machine ID: {e}") try: secure_id = machineid.hashed_id('myappid', winregistry=False) print(secure_id) except machineid.MachineIdNotFound as e: print(f"Could not determine machine ID: {e}") ``` ### Response #### Success Response - **hashed_id** (str) - The HMAC-SHA256 hex digest of the machine ID. #### Response Example ``` "366048092ef4e7db53cd7adec82dcab15ab67ac2a6b234dc6a69303a4dd48e83" ``` #### Error Response - **MachineIdNotFound** - Raised when the machine ID cannot be determined. ``` -------------------------------- ### Build Release Source: https://github.com/keygen-sh/py-machineid/blob/master/README.md Build source distribution (sdist) and wheel binary distribution for the package using `setup.py`. This prepares the package for distribution. ```bash python3 setup.py sdist bdist_wheel ``` -------------------------------- ### Run Tests Source: https://github.com/keygen-sh/py-machineid/blob/master/README.md Execute the package's tests using the `unittest` module. This is a standard way to verify the package's integrity. ```bash python3 -m unittest ``` -------------------------------- ### Publish Release Source: https://github.com/keygen-sh/py-machineid/blob/master/README.md Upload the built distribution files (located in the `dist/` directory) to a package index using `twine`. This command is used for publishing new versions. ```bash twine upload dist/* ``` -------------------------------- ### Handle MachineIdNotFound Exception with py-machineid Source: https://context7.com/keygen-sh/py-machineid/llms.txt Implement production-style error handling for cases where the machine ID cannot be determined. Includes a test case simulating failure by mocking internal helpers. ```python import machineid from unittest.mock import patch, Mock # Production-style error handling def get_device_fingerprint(app_id: str) -> str | None: try: return machineid.hashed_id(app_id) except machineid.MachineIdNotFound: # Fallback: generate a session-local ID or notify the user print("Warning: machine ID unavailable on this platform.") return None fingerprint = get_device_fingerprint('com.example.myapp') if fingerprint: print(f"Device fingerprint: {fingerprint}") # Testing: simulate a failure by mocking all internal helpers RETURNS_NONE = Mock(return_value=None) with patch.multiple('machineid', __exec__=RETURNS_NONE, __read__=RETURNS_NONE, __reg__=RETURNS_NONE): try: machineid.id() except machineid.MachineIdNotFound as e: print(f"Caught expected error: {e}") # Output: "Caught expected error: failed to obtain id on platform linux" ``` -------------------------------- ### Disable Windows Registry Query Source: https://github.com/keygen-sh/py-machineid/blob/master/README.md Both `id()` and `hashed_id()` accept a `winregistry: bool` keyword argument. Setting this to `False` disables the registry query on Windows, which can enhance security by making machine fingerprints harder to modify. ```python import machineid # Example with winregistry disabled print(machineid.id(winregistry=False)) print(machineid.hashed_id('myappid', winregistry=False)) ``` -------------------------------- ### machineid.MachineIdNotFound — Exception Handling Source: https://context7.com/keygen-sh/py-machineid/llms.txt Details the `MachineIdNotFound` exception, a subclass of `RuntimeError`, raised when the library cannot determine the machine ID on the current platform. ```APIDOC ## machineid.MachineIdNotFound ### Description `MachineIdNotFound` is a custom exception (subclass of `RuntimeError`) raised when the library is unable to determine the machine ID on the current platform. This can occur if all platform-specific detection methods fail (e.g., in a heavily restricted container environment). ### Method This is an exception class, not a callable method. ### Usage ```python import machineid try: machine_id = machineid.id() print(machine_id) except machineid.MachineIdNotFound as e: print(f"Could not determine machine ID: {e}") ``` ### Error Example ``` "failed to obtain id on platform linux" ``` ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.