Reputation: 551
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
Upvotes: 0
Views: 118
Reputation: 2810
The code you posted is rather crude, and without a lot more context it is hard to give you a good advise on how to proceed.
What you should at least do is pass data around as arguments and return values instead of using global variables. This makes the code easier to read and test.
That is:
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
It may or may not be a good idea to use a class here, that depends on what you are trying to accomplish and what your background is.
Upvotes: 0