问题
Is it possible to access the raw argv elements binary content ?
$ python3 -c'import sys;print(sys.argv);' `echo -ne "\xff\x80\x00\xff"`
['-c', '\udcff\udc80\udcff']
回答1:
You can obtain the argv contant as bytes as follows:
#!/usr/bin/python3
import sys
arg1_bytes = sys.argv[1].encode(sys.getfilesystemencoding(), 'surrogateescape')
Source: PEP 383 - Non-decodable Bytes in System Character Interfaces, via answer on "Command-line arguments as bytes instead of strings in python3"; see also answer on "sys.argv as bytes in Python 3k"
来源:https://stackoverflow.com/questions/26503817/access-command-line-arguments-as-bytes-in-python3