Commit 15fb7f7d authored by Guillaume Seguin's avatar Guillaume Seguin

Improve hostname detection and use hostname:port format

parent b149a2fd
...@@ -108,11 +108,22 @@ class printcore(): ...@@ -108,11 +108,22 @@ class printcore():
self.baud = baud self.baud = baud
if self.port is not None and self.baud is not None: if self.port is not None and self.baud is not None:
# Connect to socket if "port" is an IP, device if not # Connect to socket if "port" is an IP, device if not
p = re.compile("^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$") host_regexp = re.compile("^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$|^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$")
if p.match(port): is_serial = True
if ":" in port:
bits = port.split(":")
if len(bits) == 2:
hostname = bits[0]
try:
port = int(bits[1])
if host_regexp.match(hostname) and 1 <= port <= 65535:
is_serial = False
except:
pass
if not is_serial:
self.printer = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.printer = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.timeout = 0.25 self.timeout = 0.25
self.printer.connect((port, baud)) self.printer.connect((hostname, port))
else: else:
disable_hup(self.port) disable_hup(self.port)
self.printer = Serial(port = self.port, baudrate = self.baud, timeout = 0.25) self.printer = Serial(port = self.port, baudrate = self.baud, timeout = 0.25)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment