0

I am having a slight problem in finding a correct way to access a variable of a function in another function.

I am making a remote operations kind-of tool and so I need the command received to be processed [like 'exit' or 'nircmdc.exe' or 'telnet' etc].

The code below is not complete but it is the core:

def regular():
    global data
    data=c.recv(1024)
    data=data.decode()
    cmd=subprocess.Popen(data,shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
    output,err=cmd.communicate()
    c.sendall(output+err)

def data_process():
    data=regular().data
    quit='exit'
    nircmd='nircmdc'
    if quit in data:
    do_something()
    elif nircmd in data:
    do_else()

Here c is client connected to a socket "s" and s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)

the program gives a error nonetype has no attribute data...

Help me sort this out

4

1 回答 1

0

您发布的代码相当粗糙,如果没有更多上下文,很难就如何继续向您提供好的建议。

您至少应该做的是将数据作为参数传递并返回值,而不是使用全局变量。这使代码更易于阅读和测试。

那是:

def regular(c):
    data = c.rev()
    ...  # Code that you already have
    return data

def data_process(c):
    data = regular(c)
    ...  # Code that you already have

在这里使用课程可能是一个好主意,也可能不是一个好主意,这取决于您要完成的工作以及您的背景。

于 2013-07-23T12:40:06.283 回答