d-aro
Posts: 6
Joined: Tue May 10, 2016 8:49 am

Obexpushd GET Request through python script

Tue May 10, 2016 9:12 am

I'm having troubles using obexpushd with an attached python script through the -s switch.

The script works fine with the 'put' argument. I can easily receive files on the Raspberry Pi.

For 'get' the manpage says
This requests a specific file to be sent to stdout. Just exit the script qith a non-zero exit statis to reject the transfer

Code: Select all

# PUT request works fine.
if sys.argv[1] == 'put':
    #Get params and values
    params = {}
    while True:
        data = sys.stdin.readline()
        if data == '\n':
            break
        params[data.split(':', 1)[0]] = data.split(':', 1)[1].strip()

    sys.stderr.write(str(params) + '\n')

    #Send OK to start transfer of data
    sys.stdout.write('OK\n')
    sys.stdout.flush()

    # Write the file to disk
    f = open(params['Name'], 'w')
    data = sys.stdin.read()
    f.write(data)
    f.close()

# GET Request is called
elif sys.argv[1] == 'get':
    # Also get params first
    params = {}
    while True:
        data = sys.stdin.readline()
        if data == '\n':
            break
        params[data.split(':', 1)[0]] = data.split(':', 1)[1].strip()

    # Send file to stdout.
    # Tried with tempfile.SpooledTemporaryFile()
    # writing result to tempfile and sys.stdout.write(tempfile.read())
    result = 'Some data to send to Client'
    sys.stdout.write(result)
    sys.stdout.flush()
   
The error I'm getting is

Code: Select all

0.1: OBEX_EV_REQ, OBEX_CMD_GET
0.1: Got header 0x01 with value length 14
0.1: name: "status"
0.1: Got header 0x42 with value length 16
0.1: type: "x-mobile/status"
0.1: Running script failed or no output data: Invalid argument
0.1: Sending response code 500
0.1: OBEX_EV_STREAMEMPTY, OBEX_CMD_CONNECT
0.1: Sending response code 500
0.1: OBEX_EV_LINKERR, OBEX_CMD_GET
If I use print instead of sys.stdout.write I get

Code: Select all

0.1: Running script failed or no output data: Success
Hopefully someone can assist to get this right.
Maybe I'm just doing some little error. Hopefully..

Thanks all for your help

d-aro

d-aro
Posts: 6
Joined: Tue May 10, 2016 8:49 am

Re: Obexpushd GET Request through python script

Fri May 13, 2016 2:47 pm

Got it working with Obexpushd creator's help.

It's realy simple. I was just missing to send a Length Header before the data.

Code: Select all

    # Send file to stdout.
    result = 'Some data to send to Client'
    lengthHeader = 'Length: ' + str(len(result))
    sys.stdout.write(lengthHeader + '\n)
    sys.stdout.write('\n')      # New Line after the Header is needed
    sys.stdout.write(result)
    sys.stdout.flush()

Return to “Python”