I'm using a NamedTuple to define a server to which I want to connect using telnetlib. Then I created a class which defines the connection to the server, with the server details and connection method inside the class. Then outside of the class, I want to use the connection method with the server's NamedTuple as connection credentials. However I keep getting the Error that the connection method is missing the NamedTuple argument.
I've tried pulling the NamedTuple outside of the class, tried to put the Namedtuple inside the init method of the class. nothing seems to work.
This is my code:
import telnetlib
from typing import NamedTuple
class Unit(NamedTuple):
name: str
ip: str
port: str
def printunit(self, unit):
print(unit.name)
print(unit.ip)
print(unit.port)
class TnCnct:
Server1 = Unit("Server1", "1.1.1.1", "23")
Server2 = Unit("Server2", "2.2.2.2", "23")
Server3 = Unit("Server3", "3.3.3.3", "23")
def __init__(self):
pass
def cnct(self, u):
try:
tn = telnetlib.Telnet(u.ip, u.port, 10)
tn.open(u.ip, u.port)
tn.close()
response = u.name + " " + "Success!"
except Exception as e:
response = u.name + " " + "Failed!"
print(e)
finally:
print(response)
TnCnct.cnct(TnCnct.Server1)
The exact Error I get:
TypeError: cnct() missing 1 required positional argument: 'u'