Search code examples
python-3.xsyntaxsyntax-errortelnetlib

How to use a variable in session.write


So, I’m using Telnetlib with Python 3 to try and create a software replay station, and I’m trying to modify one of the telnet commands to change the time code based on a variable, but I keep getting a syntax error since the only way I can get it to work is encoding the command as bytes

TC=5 #variable is changed by the GUI
session.write(b”play: timecode: 00:00:0”TC”;00 \nc”

Any idea how to format this with the proper syntax

Part 2: Here's the scale box I'm trying to use to modify

self.IRpreroll = tk.Scale(top, from_=0.0, to=10.0) self.IRpreroll.place(relx=0.659, rely=0.143, relwidth=0.317, relheight=0.0, height=59, bordermode='ignore') self.IRpreroll.configure(activebackground="#ececec") self.IRpreroll.configure(background="#282828") self.IRpreroll.configure(font="TkTextFont") self.IRpreroll.configure(foreground="#828282") self.IRpreroll.configure(highlightbackground="#d9d9d9") self.IRpreroll.configure(highlightcolor="black") self.IRpreroll.configure(label="preroll time") self.IRpreroll.configure(orient="horizontal") self.IRpreroll.configure(troughcolor="#828282") self.IRpreroll.configure(variable=testguiPAGE_support.Ptime)

which then references this in the support script


Solution

  • Create string and later encode it to bytes

    TC = 5 
    
    text = "play: timecode: 00:00:0" + str(TC) + ";00 \\nc"
    # or
    text = "play: timecode: 00:00:0{};00 \\nc".format(TC)
    # or
    text = f"play: timecode: 00:00:0{TC};00 \\nc" # Python 3.6+
    
    session.write( text.encode() )
    

    It string you have to use \\ to get \ in bytes