0

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.

Community
  • 1
  • 1
Sven
  • 55
  • 6
  • Do you have non ascii characters in the JSON file? – Paulo Bu Jan 22 '14 at 20:56
  • @PauloBu I don't believe so. The JSON file coming from the javascript is actually one that was passed to it from an earlier python script, just with one or two parameters changed. Does javascript automatically insert a non-ascii end-of-string character into strings? If so that could be the issue I suppose, as the parameters that I change are changed to strings, but they're all formatted numbers. – Sven Jan 22 '14 at 21:18
  • Having `\xff` at the very beginning of the file probably means that the file is store with utf16 byte order mask little endian (`0xFFEF`. How is the file saved to disk? – Paulo Bu Jan 22 '14 at 21:25
  • @PauloBu The script is saved on an Apache server running on a raspberry pi. It was written on a Windows machine, so before moving it to the server, I used CygWin's dos2unix utility to format it for the unix system. I have it stored in the same directory as all the other scripts, which work correctly. I'm not sure if this was the answer you were looking for, if not, could you clarify what you mean a bit? – Sven Jan 22 '14 at 21:37
  • I'm not sure, I don't have enough info sorry :( Just try to figure out in which encoding the file is written and also (just in case) try to see if there is a `ÿ` character wandering around in the file. – Paulo Bu Jan 22 '14 at 21:41

0 Answers0