首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我可以在Python中通过eventlet.backdoor调用函数或更改变量吗?

我可以在Python中通过eventlet.backdoor调用函数或更改变量吗?
EN

Stack Overflow用户
提问于 2012-01-27 10:54:20
回答 3查看 721关注 0票数 0

我写了这个最小的代码来解释我的案例:

代码语言:javascript
复制
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()。但它不起作用。是我搞错了,还是不可能?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-01-28 05:35:32

正如fthinker在上面的评论中所说的:

看起来后门服务器并不使用相同的名称空间。键入函数名称会显示它们是未定义的,您的变量'should_printing‘也是未定义的。我在远程登录到后门服务器设置的解释器时对此进行了测试。

(如果fthinker回复为回复帖子,我将删除此帖子)

票数 -1
EN

Stack Overflow用户

发布于 2015-05-18 19:43:53

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

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2014-09-04 14:23:12

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

代码语言:javascript
复制
the executing script runs in a module named __main__, importing the
script under its own name will create a new module unrelated to
__main__.
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9028423

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档