Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从字符串中提取命令-python_Python - Fatal编程技术网

如何从字符串中提取命令-python

如何从字符串中提取命令-python,python,Python,我有这样一个命令: test = ('print( "It works!" )' ) 我想使用字符串中的命令。 你知道我如何提取和使用它吗 多谢各位, Remus使用exec,但要确保您信任该代码,即出于安全原因,不要在用户提交的代码上运行此操作 >>> test = ('print( "It works!" )' ) >>> exec test It works! 如果不能信任字符串,请指出为什么这是坏的,请考虑测试=“Del-SuxBufftIsIsx

我有这样一个命令:

test = ('print( "It works!" )' )
我想使用字符串中的命令。 你知道我如何提取和使用它吗

多谢各位,
Remus

使用
exec
,但要确保您信任该代码,即出于安全原因,不要在用户提交的代码上运行此操作

>>> test = ('print( "It works!" )' )
>>> exec test
It works!

如果不能信任字符串,请指出为什么这是坏的,请考虑<代码>测试=“Del-SuxBufftIsIsx”< /代码>。它又短又甜,它削弱了python——但不会削弱计算机,这是真正的攻击者试图做的事情之一。