0

I am sure this is a fairly noob question, I have googled, and cannot find a straight answer, but I may be asking the wrong thing... I am trying to make an Out Of Box Configuration script, and all the questions that need answered are stored in a file called pass.ini. When i get user input from getstr (using curses) when it populates my files, they all have b'variable string' as their values. when I try to do a strip command, I get b'riable strin'. when I do a str(variable) it gets the same issue. I saw where b'<variable_string>' can be a sign that it was in bytecode instead of decoded. so I tried a decode command and that failed as 'str object has no attribute 'decode' I have it writing out via ConfigParser, and to a separate file just as a file.write. Right now everything is commented out, I am out of ideas.

Here is the info gathering module:

        wrapper(CommitChanges)
            curses.echo()
            stdscr.addstr(  8, 19,  config.CIP , curses.color_pair(3) )
            config.CIP = stdscr.getstr(  8, 19, 15)
            stdscr.addstr(  9, 19,  config.CSM , curses.color_pair(3) )
            config.CSM = stdscr.getstr( 9, 19, 15)
            stdscr.addstr( 10, 19,  config.CGW , curses.color_pair(3) )
            config.CGW = stdscr.getstr(10, 19, 15)
            stdscr.addstr( 11, 19,  config.CD1 , curses.color_pair(3) ) 
            config.CD1 = stdscr.getstr(11, 19, 15)
            stdscr.addstr( 12, 19,  config.CD2 , curses.color_pair(3) )
            config.CD2 = stdscr.getstr(12, 19, 15)
            stdscr.addstr( 13, 19,  config.CNTP, curses.color_pair(3) )
            config.CNTP = stdscr.getstr(13, 19, 15)
            stdscr.addstr( 16, 19,  config.CHN , curses.color_pair(3) )
            config.CHN = stdscr.getstr(16, 19, 15)
            stdscr.addstr( 14, 19,  config.CID , curses.color_pair(3) )
            config.CID = stdscr.getstr(14, 19, 15)
            stdscr.addstr( 15, 19,  config.CS , curses.color_pair(3) )
            config.CS = stdscr.getstr(15, 19, 15)


This is the file output module

def CommitChanges():
    MOP = "X"
    Config['Array=all']['PTLIP']        = a
    Config['Array=all']['PTLSM']        = config.CSM.decode('utf-8')
    Config['Array=all']['PTLGW']        = config.CGW.decode('utf-8')  
    Config['Array=all']['PTLD1']        = config.CD1.decode('utf-8')
    Config['Array=all']['PTLD2']        = config.CD2.decode('utf-8') 
    Config['Array=all']['PTLNTP']       = config.CNTP.decode('utf-8') 
    Config['Array=all']['PTLIF']        = config.CIFN.decode('utf-8') 
    Config['Array=all']['PTLHSTNM']     = config.CHN.decode('utf-8') 
    Config['Array=all']['PTLMOB']       = config.CMOB.decode('utf-8')
    Config['Array=all']['customerid']   = config.CID.decode('utf-8')
    Config['Array=all']['site']         = config.CS.decode('utf-8')
    with open('/opt/passp/pass.ini', 'w') as passini:
        Config.write(passini, space_around_delimiters=False)
    tpass= open('./pass.b', 'w')
    tpass.write("[Array=All]"+ "\n")
    tpass.write("ptlip="+ a + "\n")
    tpass.write("ptlsm="+ config.CSM.decode('utf-8') +"\n")
    tpass.write("ptlgw="+ config.CGW.decode('utf-8') + "\n")
    tpass.write("ptld1="+ config.CD1.decode('utf-8') + "\n")
        tpass.write("ptld2="+ config.CD2.decode('utf-8') + "\n")
    tpass.write("ptlntp="+ config.CNTPdecode('utf-8') + "\n")
    tpass.write("ptlif="+ config.CIFNdecode('utf-8') + "\n")
    tpass.write("ptldhstnm="+ config.CHNdecode('utf-8') + "\n")
    tpass.write("ptlmob="+ config.CMOBdecode('utf-8') + "\n")
    tpass.write("customerid="+ config.CIDdecode('utf-8') + "\n")
    tpass.write("site="+ config.CSdecode('utf-8') + "\n")
    #if Backupfiles():
    textchanges()
    return

Here is the file save output created by ConfigParser

[Array=all]
ptlip=b'123'
ptlsm=b'321'
ptlgw=b'111'
ptld1=b'222'
ptld2=b'333'
ptlntp=b'444'
ptlif=s19
ptlhstnm=b'555'
ptlmob=
customerid=b'666'
site=b'777'

It perfectly matches when I do a direct write (they were from two different runs, but even with empty data it has the wrapper.

Interesting notice here, 'ptlif' is gathered from finding the interface name, it isn't handled by user input, so it has to be how the config.XXXX variables are stored.


[Array=All]
ptlip=b''
ptlsm=b''
ptlgw=b''
ptld1=b''
ptld2=b''
ptlntp=b''
ptlif=s19
ptldhstnm=b''
ptlmob=
customerid=b''
site=b''
  • Programming questions are not on topic here. You can ask such questions on our sister site, [so]. – Michael Hampton Sep 21 '20 at 22:05
  • Sorry, new to the boards, I thought I had requested this on Stack Overflow, I didn't even have an account on this one. I will ask it over there. We can delete this, if need be. – Kevin Rutan Sep 22 '20 at 09:20

1 Answers1

-1

Though this is a programming question, the b'' indicates that it is in bytes. A big change from python 2.

str corresponds to the former unicode type on Python 2. It is represented internally as a sequence of Unicode codepoints. You can declare a str variable without prepending the string with u, because it is default now.

bytes roughly corresponds to the former str type (for the bytes part) on Python 2. It is a binary serialization format represented by a sequence of 8-bits integers that is fit for storing data on the filesystem or sending it across the Internet. That is why you can only create bytes containing ASCII literal characters. To define a bytes variable, just prepend a b to the string.

Petey_Woof
  • 79
  • 4
  • Please don't answer off-topic questions. It just means some poor moderator has to come along and delete it. It also negatively affects your account. – Michael Hampton Sep 22 '20 at 10:45