Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Escaped string of bytes to bytestring

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?

like image 841
isklenar Avatar asked Nov 28 '25 23:11

isklenar


1 Answers

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.

like image 52
Martijn Pieters Avatar answered Nov 30 '25 11:11

Martijn Pieters



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!