python-3.x - 我的语法 read().encode("hex") 在 python 3 中不起作用
问题描述
def run_action(self):
if os.path.exists(self.vars["source"][0]):
self.ui.print_msg("Shellcode:")
print ("\\x" + "\\x".join(re.findall("..", open(self.vars["source"][0], "rb").read().encode("hex"))))
else:
self.ui.print_error("%s not found" % self.vars["source"][0])
这是该功能不适用于 Python3:
print ("\\x" + "\\x".join(re.findall("..", open(self.vars["source"][0], "rb").read().encode("hex"))))
我在上面的行中遇到错误。你能帮我按照python3语法写这个函数吗?
解决方案
看起来您想将字节字符串打印为十六进制转义码。Python 3 不再支持.encode('hex')
,但在 Python 2 和 3 中有更好的方法来实现这一点:
蟒蛇 2.7:
>>> data = 'data'
>>> print(''.join(['\\x{:02x}'.format(ord(b)) for b in data]))
\x64\x61\x74\x61
Python 3.6+:
>>> data = b'data'
>>> print(''.join([f'\\x{b:02x}' for b in data]))
\x64\x61\x74\x61
推荐阅读
- javascript - 在 React 中合并数据的最佳方法
- vue.js - JS - 如何实现 YTD(年初至今)逻辑以进行日期选择
- python - 使用 Tkinter 对齐每行中的按钮和标签
- javascript - 如何将变量从客户端 NodeJS 传递到服务器端?
- jenkins - 在推送新提交时,在 Jenkins 多分支插件中取消先前的拉取请求构建
- python - 如何在大文件中查找字符串的索引并在某个索引处请求字符串?
- python - 如何在 Python 列表中获取最大持续时间
- ios - UICollectionView 中的交错布局(iOS 10 及更高版本) - iOS Swift
- node.js - 无人看管循环时,console.log() 被“取消”
- python - 尝试使用 if/else 语法返回用户输入时,“必须是实数,而不是 NoneType”