NetworkingUtil.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. # Copyright (c) 2019 Ultimaker B.V.
  2. # Cura is released under the terms of the LGPLv3 or higher.
  3. import socket
  4. from typing import Optional
  5. from PyQt5.QtCore import QObject, pyqtSlot
  6. #
  7. # This is a QObject because some of the functions can be used (and are useful) in QML.
  8. #
  9. class NetworkingUtil(QObject):
  10. def __init__(self, parent: Optional["QObject"] = None) -> None:
  11. super().__init__(parent = parent)
  12. # Checks if the given string is a valid IPv4 address.
  13. @pyqtSlot(str, result = bool)
  14. def isIPv4(self, address: str) -> bool:
  15. try:
  16. socket.inet_pton(socket.AF_INET, address)
  17. result = True
  18. except:
  19. result = False
  20. return result
  21. # Checks if the given string is a valid IPv6 address.
  22. @pyqtSlot(str, result = bool)
  23. def isIPv6(self, address: str) -> bool:
  24. try:
  25. socket.inet_pton(socket.AF_INET6, address)
  26. result = True
  27. except:
  28. result = False
  29. return result
  30. # Checks if the given string is a valid IPv4 or IPv6 address.
  31. @pyqtSlot(str, result = bool)
  32. def isValidIP(self, address: str) -> bool:
  33. return self.isIPv4(address) or self.isIPv6(address)
  34. __all__ = ["NetworkingUtil"]