mirror of
https://github.com/Ultimaker/Cura.git
synced 2025-07-16 03:07:53 -06:00
Make a single NetworkingUtil
CURA-6483
This commit is contained in:
parent
bb1442a04e
commit
ed8127777c
5 changed files with 48 additions and 57 deletions
44
cura/Utils/NetworkingUtil.py
Normal file
44
cura/Utils/NetworkingUtil.py
Normal file
|
@ -0,0 +1,44 @@
|
|||
# Copyright (c) 2019 Ultimaker B.V.
|
||||
# Cura is released under the terms of the LGPLv3 or higher.
|
||||
|
||||
import socket
|
||||
from typing import Optional
|
||||
|
||||
from PyQt5.QtCore import QObject, pyqtSlot
|
||||
|
||||
|
||||
#
|
||||
# This is a QObject because some of the functions can be used (and are useful) in QML.
|
||||
#
|
||||
class NetworkingUtil(QObject):
|
||||
|
||||
def __init__(self, parent: Optional["QObject"] = None) -> None:
|
||||
super().__init__(parent = parent)
|
||||
|
||||
# Checks if the given string is a valid IPv4 address.
|
||||
@pyqtSlot(str, result = bool)
|
||||
def isIPv4(self, address: str) -> bool:
|
||||
try:
|
||||
socket.inet_pton(socket.AF_INET, address)
|
||||
result = True
|
||||
except:
|
||||
result = False
|
||||
return result
|
||||
|
||||
# Checks if the given string is a valid IPv6 address.
|
||||
@pyqtSlot(str, result = bool)
|
||||
def isIPv6(self, address: str) -> bool:
|
||||
try:
|
||||
socket.inet_pton(socket.AF_INET6, address)
|
||||
result = True
|
||||
except:
|
||||
result = False
|
||||
return result
|
||||
|
||||
# Checks if the given string is a valid IPv4 or IPv6 address.
|
||||
@pyqtSlot(str, result = bool)
|
||||
def isValidIP(self, address: str) -> bool:
|
||||
return self.isIPv4(address) or self.isIPv6(address)
|
||||
|
||||
|
||||
__all__ = ["NetworkingUtil"]
|
Loading…
Add table
Add a link
Reference in a new issue