在python3中将命令行参数作为字节访问 [英] Access command line arguments as bytes in python3

查看:185
本文介绍了在python3中将命令行参数作为字节访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以访问原始argv元素的二进制内容?

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']

推荐答案

您可以按以下方式获取argv contant字节:

You can obtain the argv contant as bytes as follows:

#!/usr/bin/python3
import sys
arg1_bytes = sys.argv[1].encode(sys.getfilesystemencoding(), 'surrogateescape')

来源: PEP 383-系统字符接口中的不可解码字节,通过回答命令行参数以字节而不是python3中的字符串"的方式;另请参见关于"sys.argv作为Python 3k中的字节"的答案

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"

这篇关于在python3中将命令行参数作为字节访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆