我写了这个最小的代码来解释我的情况:
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()。 但它不起作用。我犯了错误,还是不可能?
答案 0 :(得分: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()
现在,应该在端口3000上运行的python解释器上看到should_printing,并将其设置为false将停止打印
答案 1 :(得分:0)
您无法访问should_printing
,因为__main__
模块与导入的模块不同,即使它们是同一模块也是如此。查看详细信息here
the executing script runs in a module named __main__, importing the
script under its own name will create a new module unrelated to
__main__.
答案 2 :(得分:-1)
正如fthinker在上面的评论中所说:
后门服务器使用相同的命名空间看起来并不像。 键入函数名称表示它们是未定义的和您的变量 ' should_printing'也是未定义的。我在telnetted时测试了这个 进入后门服务器设置的解释器。
(如果fthinker回复作为回复帖我将删除此帖)