Question

I have a CGI script in python that uses Telnetlib to send telnet commands. I get appropriate information (in this case the 'nameID' from the javascript in the form of a JSON, and then send the appropriate commands with regard to the information fromt that JSON. When I attempt to write and send the commands to telnet, however, I get a UnicodeDecodeError: 'ascii' codec can't decode byte 0xff in position 0: ordinal not in range(128) message.

My code is as follows:

import cgi,sys,json,multiprocessing,telnetlib

def updateParam(subjson):
    tn=telnetlib.Telnet()
    tn.open(subjson["netAddress"],24,1)

    logFile=open("log.txt","a")

    tn.write("saveparam nameID "+subjson["nameID"]+"\n")
    logFile.write(tn.read_until("#"))

    tn.close()
    logfile.close()



if __name__=="__main__":
    data=json.load(sys.stdin)

    numProc=4
    pool=multiprocessing.Pool(processes=numProc)

    for dev in data:
        result=pool.apply_async(updateParam,args=(data[dev],))
        result.get()
    pool.close()
    pool.join()

In the above example, I load the JSON passed to the script (which itself contains JSONs as its elements) into the data variable. Then, I create a pool of workers to perform the updateParam function on each JSON within data, as each one of those corresponds to an individual telnet address two which commands need to be sent. Within updateParam, I open a telnet connection to the device's IP address, as well as opening a .txt file to keep a log of what is sent and received. I write the command to the telnet connection, and that's where I hit the error.

I am able to send commands that don't require a value from the json, but when I try to use the json value, I get the UnicodeDecodeError. I am able to write the values to my .txt file as well without issue.

Why am I getting this error specifically when I try to write those values to a telnet connection?

EDIT: I went into the javascript that sends the JSON through cgi and attempted to remove all non-ascii characters from the json before sending the cgi request. This did not work. I was unable to find any non-ascii characters (in particular the ÿ that seemed to be causing problems) when I had the javascript alert the stringified json before sending the request as well. Then I implemented the solution found in this question and it appears to be working correctly.

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top