I have the following code:
try:
tn = Telnet(host, str(port))
except Exception as e:
print("Connection cannot be established", e)
traceback.print_exc()
print("You are connected")
tn.write('command?'+'\r\n')
while True:
line = tn.read_until("\n")
When I run this code on machine X everything is working just fine, but when when I try to run the same code on a different machine I end up with the following error:
Traceback (most recent call last):
File
"C:/Users/admin/Documents/Projects/terminalManager/terminalManager.py", line 50, in <module>
terminalManager()
File
"C:/Users/admin/Documents/Projects/terminalManager/terminalManager.py", line 16, in __init__
self.connect(terminalBganIp, terminalBganPort)
File "C:/Users/admin/Documents/Projects/terminalManager/terminalManager.py", line 34, in connect
tn.write('AT_IGPS?'+'\r\n')
File "C:\Program Files (x86)\Python\Python3.6.1\lib\telnetlib.py", line 287, in write
if IAC in buffer:
TypeError: 'in <string>' requires string as left operand, not bytes
Am I doing something wrong or is the second machine messing with me?
EDIT:
When i used IDLE debugger on my second machine everything is working. it seems it is not working when running it normally, is there anything i can do to resolve this?
I can't believe that the same code is working for you on another machine with the same python version.
Your issue is exactly what the Exception says it is TypeError: 'in <string>' requires string as left operand, not bytes. You need to provide bytes to tn.write instead of a string.
You can convert your string into bytes via encode:
command = "command?" + "\r\n"
tn.write(command.encode("ascii"))
Edit: Well, someone beat me to it :D
Try to do:
tn.write(('command?'+'\r\n').encode())
Usually sockets work over bytes not strings, the error could be related to that, hope this can help.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With