0

我写了这个最小的代码来解释我的情况:

import threading
import time
import eventlet
from eventlet import backdoor

eventlet.monkey_patch()

global should_printing
should_printing = True

def turn_off_printing():
    global should_printing
    should_printing = not should_printing

def printing_function():
    global should_printing
    while should_printing == True:
        print "printing"
        time.sleep(1)

def console():
    while True:
        print "inside console"
        time.sleep(1)

if __name__ == '__main__':
    eventlet.spawn(backdoor.backdoor_server, eventlet.listen(('localhost', 3000)))

    thread = threading.Thread(target=printing_function)
    thread.start()

    thread = threading.Thread(target=console)
    thread.start()

执行后,我通过 telnet 连接,导入我的模块并调用 turn_off_printing()。但它不起作用。我犯了错误,还是不可能?

4

3 回答 3

1

确保在调用后门程序时传递所有你想访问的变量/函数

if __name__ == '__main__':
    s=eventlet.spawn(backdoor.backdoor_server, eventlet.listen(('localhost', 3000)), globals())

    thread1 = threading.Thread(target=printing_function)
    thread1.start()

    s.wait()

现在 should_printing 应该在端口 3000 上运行的 python 解释器上可见,并将其设置为 false 将停止打印

于 2015-05-18T11:43:52.943 回答
0

您无法访问should_printing,因为__main__模块与导入的模块不同,即使它们是相同的模块。在此处查看详细信息

the executing script runs in a module named __main__, importing the
script under its own name will create a new module unrelated to
__main__.
于 2014-09-04T06:23:12.147 回答
-1

As fthinker said in comment above:

It doesn't look like the backdoor server is using the same namespace. Typing the function names said they were undefined and your variable 'should_printing' is undefined as well. I tested this while telnetted into the interpreter set up by the backdoor server.

(if fthinker reply as answer post i will delete this post)

于 2012-01-27T21:35:32.160 回答