ハロー ブログ

日々のつぶやき @c2c2c2c221

デバッグ中の記述

def send_command(remote, filename='REMOTE.LOG'):</span>
    # < little-endian
    # h short integer standard size (2 byte)
    print("### debug send_command")
    f=open(filename,'wb')
    print("### debug 000-0")
    f.write(remote)
    print("### debug 000-1")
    f.close()
    print("### debug 000-2")
    # upload file
    try:
        print("### debug 000-10")
        debuglevel = 0
        #cookies = cookielib.CookieJar()
        cookies = http.cookiejar.CookieJar()
        print("### debug 000-10-1")
        #opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookies),
        #                              urllib2.HTTPHandler(debuglevel=debuglevel),
        #                              MultipartPostHandler)
        opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cookies),
                                             urllib.request.HTTPHandler(debuglevel=debuglevel),
                                             MultipartPostHandler)
        # check if upload is enabled
        print("### debug 000-11")
        command_cgi = 'http://flashair/command.cgi'
        get_params = {'op' : 108}
        print("### debug 000-12")
        print("### debug:" + command_cgi + '?' + urllib.parse.urlencode(get_params) )
        #response = opener.open(command_cgi + '?' + urllib.parse.urlencode(get_params))
        print("### debug 000-13")
        #print("### flashair firmware version : " + response.read())
        get_params = {'op' : 118}
        #response = opener.open(command_cgi + '?' + urllib.urlencode(get_params))
        #print("         check upload enabled : " + response.read())
        # send command
        print("### debug 000-14")
        #print("         sending    {:s} : {}".format(filename, " ".join(map("%2.2x".__mod__, map(ord,remote)))))
        upload_cgi = 'http://flashair/upload.cgi'
        #date = datetime.date.today()
        #date = datetime.date()
        print("### debug 000-14-1")
        #ftime = '0x{0:x}{1:x}'.format((((date.year - 1980) ; 9) + ((date.month + 0) ; 5) + date.day),
        #                              ((date.hour ; 11) + (date.minute ; 5) + (date.second / 2)))
        ftime = '0x5367b232'
        print("### debug 000-14-2 : " + ftime )
        #get_params = {'WRITEPROTECT' : 'OFF', 'UPDIR' : '/ADDON/', 'FTIME' : ftime}
        get_params = {'UPDIR' : '/ADDON/' , 'FTIME' : ftime}
        post_params = { "file" : open(filename, "rb") }
        #print("### debug 000-15-0-1 " + upload_cgi + '?' + urllib.parse.urlencode(get_params))
        print("### debug 000-15-0-1 : " + upload_cgi + '?' + urllib.parse.urlencode(get_params))
        #print("### debug 000-15-0-2 : " + post_params)
        #response = opener.open(upload_cgi + '?' + urllib.parse.urlencode(get_params))
        #response = opener.open('http://flashair/upload.cgi?UPDIR=/ADDON/&FTIME=0x5367b232')
        print("### debug 000-15-0-3 : ")
        stream = os.popen('curl  "http://flashair/upload.cgi?UPDIR=/ADDON/&FTIME=0x5367b232"')
        output = stream.read()
        print(output)
        #echo -en "\xfe\xff\x17\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" >REMOTE.LOG
        #curl --form "file=@REMOTE.LOG" "http://flashair/upload.cgi"
        print("### debug 000-15-0-4 : ")
        #subprocess.run('echo -en ','\"\xfe\xff\x17\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00 \"' , ' >REMOTE.LOG')
        #stream = os.popen(["echo -en ", "\xfe\xff\x17\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", " >REMOTE.LOG"])
        stream = os.popen('curl --form "file=@REMOTE.LOG" "http://flashair/upload.cgi"')
        output = stream.read()
        print(output)
        print("### debug 000-15-0-5 : ")
        #stream = os.popen('curl --form \"file=@REMOTE.LOG\" \"http://flashair/upload.cgi\"')
        output = stream.read()
        print(output)
        #response = opener.open("http://flashair/rvc/n.thm")
        print("### debug 000-15-1")
        #if response.read() != "SUCCESS":
        #if output != "SUCCESS":
        #    raise Exception("fail to open upload.cgi")
        print("### debug 000-16")
        response = opener.open(upload_cgi, post_params)
        opener.close()
        soup = BeautifulSoup(response)
        if soup.html.h1.string != "Success":
            raise Exception(soup.prettify())
        print("         ... Successed")
    #except urllib2.URLError(e):
    except urllib.error.URLError as e :
        print("### debug 001")
        print(e.reason)
        return False
    except Exception as e:
        print("### debug 002")
        print(e)
        return False
    except:
        print("### debug 003")
        print(sys.exc_info())
        return False
    return True