2

I'm new to Python and I need to read bytestring from a command line argument. I'm using Python 3.4.

At the moment, I'm using argparse to parse the arguments, with this configuration for the data: parser.add_argument("-d", "--data", default=b'\0')

When I call my program with -d argument (e.g. python myprogram.py -d b'd\x00!\x00W\x00'), it interprets the value of -d as a string, escaping slashes and treating the 'b' as part of the string, like this: 'b\\'d\\x00!\\x00W\\x00\\''

Is there a way to unescape the output from argparse and convert it to bytes?

isklenar
  • 974
  • 2
  • 14
  • 34

1 Answers1

2

You'd normally have the shell formulate the exact bytes, but since you cannot pass in NUL bytes as arguments asking users to pass in escape sequences is a reasonable work-around.

However, the shell is not going to interpret Python byte string literal notation.

In this case, I'd ask the user to enter hexadecimal values instead:

python myprogram.py -d "64 00 21 00 57 00"

and use the binascii.unhexlify() function to produce your bytes value from that (removing any whitespace first):

whitespace = dict.fromkeys((9, 10, 13, 32))  # tab, space, newline and carriage return
data = binascii.unhexlify(args.data.translate(whitespace))

This does require that you set your default argument value to a compatible value:

parser.add_argument("-d", "--data", default='00')

The alternative would be to use the ast.literal_eval() function to interpret the Python byte string literal syntax:

data = ast.literal_eval(args.data)

and your default'd be:

parser.add_argument("-d", "--data", default=repr(b'\0'))

but take into account that this function accepts any Python literal, so you could end up with any other object type, including numbers, strings and containers.

Community
  • 1
  • 1
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • 1
    @thefourtheye: which doesn't help anything here because that doesn't handle interpreting escape sequences. You also still cannot pass in a NUL byte as `sys.argv` value. – Martijn Pieters May 03 '15 at 08:53
  • Yup, I was about to add that bug was not helpful, but my browser won't allow me for some reason. I ll remove that. – thefourtheye May 03 '15 at 08:57