我可以访问已经可执行的Python脚本的代码吗?
例如,我运行了script test.py,并意外地更改了可执行文件,但该文件中的脚本继续工作。 在顶部,我可以看到执行“python3test.py”的是什么,但它并不完全正确。我可以访问已经可执行的Python脚本的代码吗?,python,python-3.x,Python,Python 3.x,例如,我运行了script test.py,并意外地更改了可执行文件,但该文件中的脚本继续工作。 在顶部,我可以看到执行“python3test.py”的是什么,但它并不完全正确。 我可以访问更改后执行的代码吗?恢复源代码的一种可能方法是使用工具和 Pyrasite用于将代码注入Python进程,inspect用于检索模块、类、方法、函数、回溯、帧对象和代码对象的源代码 例如。 初始文件:foobar.py import time def test1(): print "test1"
我可以访问更改后执行的代码吗?恢复源代码的一种可能方法是使用工具和 Pyrasite用于将代码注入Python进程,inspect用于检索模块、类、方法、函数、回溯、帧对象和代码对象的源代码 例如。 初始文件:foobar.py
import time
def test1():
print "test1"
if __name__ == "__main__":
while (1):
time.sleep(1)
test1()
运行并修改文件(我通过“test2”更改了打印的字符串):
它仍在打印test1
,如果使用pyrasite并进行检查,则可以看到它正在运行的函数的源代码
$ pyrasite-shell 6243
Pyrasite Shell 2.0
Connected to 'python foobar.py'
Python 2.7.9 (default, Jun 29 2016, 13:08:31)
[GCC 4.9.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
(DistantInteractiveConsole)
>>> import inspect
>>> inspect.getsource(test1)
'def test1():\n\tprint "test1"\n'
另外,如果您也删除了该文件,它也可以工作。您的答案很好,我只是有一个小问题,当我尝试调用inspect(example_function)(是的,我对此名称感到抱歉)时,其他地方定义的函数被称为('/usr/local/lib/python3.5 dist packages/pytg/utils.py),而不是在源文件中,我对@coroutine def example_function(sender)的定义:我想你应该导入这个模块
$ pyrasite-shell 6243
Pyrasite Shell 2.0
Connected to 'python foobar.py'
Python 2.7.9 (default, Jun 29 2016, 13:08:31)
[GCC 4.9.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
(DistantInteractiveConsole)
>>> import inspect
>>> inspect.getsource(test1)
'def test1():\n\tprint "test1"\n'