网站返回的字节里有16进制,如何在python转成字符串.

b'{"action":"auth","msg":"eval(\\"\\\\x4d\\\\x61\\\\x74\\\\x68\\\\x2e\\\\x63\\\\x6f\\\\x73\\\\x28\\\\x31\\\\x35\\\\x31\\\\x35\\\\x37\\\\x37\\\\x38\\\\x31\\\\x34\\\\x30\\\\x29\\")"}'

上面是网站返回的数据, 不知道在python里怎么把上面转为Math.cos(1515778140)
我能得到Math.cos(1515778140)这个结果的步骤是:

eval(\\"\\\\x4d\\\\x61\\\\x74\\\\x68\\\\x2e\\\\x63\\\\x6f\\\\x73\\\\x28\\\\x31\\\\x35\\\\x31\\\\x35\\\\x37\\\\x37\\\\x38\\\\x31\\\\x34\\\\x30\\\\x29\\")

手动转为

alert("\x4d\x61\x74\x68\x2e\x63\x6f\x73\x28\x31\x35\x31\x35\x37\x37\x38\x31\x34\x30\x29")

放到控制台console执行后得到的, 求问如何在python得到Math.cos(1515778140)

阅读 2k
1 个回答
# -*- coding: utf-8 -*-

import json

msg = b'{"action":"auth","msg":"eval(\\"\\\\x4d\\\\x61\\\\x74\\\\x68\\\\x2e\\\\x63\\\\x6f\\\\x73\\\\x28\\\\x31\\\\x35\\\\x31\\\\x35\\\\x37\\\\x37\\\\x38\\\\x31\\\\x34\\\\x30\\\\x29\\")"}'
msg = json.loads(msg)['msg']

#复杂点的
import re
print ''.join(chr(int(s, 16)) for s in re.findall(r'\\x(.{2})', msg))

#简单点的
print eval(msg.replace('eval', ''))
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题