[Twisted-Python] Problem with process ?
Hello, I have problem with getProcessOutput, I attempt this code (*) and i change the line : return utils.getProcessOutput("ls",["-l", "-a"]) by : return utils.getProcessOutput("ls",["-l, "."]) it's Ok for this... But when i change by : return utils.getProcessOutput("ls",["-l *"]) or : return utils.getProcessOutput("ls",["-l -a"]) or : return utils.getProcessOutput("ls",["-l ."]) and i have allways this error, but I don't undestand ?? *--- Failure #5 --- --- <exception caught here> --- *--- End of Failure #5 --- Traceback (most recent call last): File "/home/foo/twisted/internet/default.py", line 526, in doSelect ....etc.. "/home/foo/twisted/internet/utils.py", line 44, in errReceived self.deferred.errback(failure.Failure(IOError("got stderr"))) exceptions.AttributeError: 'NoneType' object has no attribute 'errback' *--- Failure #9 --- --- <exception caught here> --- *--- End of Failure #9 --- Traceback (most recent call last): File "/home/foo/twisted/internet/default.py", line 526, in doSelect _logrun(selectable, _drdw, selectable, method, dict) ......etc.. File "/home/foo/twisted/internet/utils.py", line 44, in errReceived self.deferred.errback(failure.Failure(IOError("got stderr"))) exceptions.AttributeError: 'NoneType' object has no attribute 'errback' *--- Failure #13 --- --- <exception caught here> --- *--- End of Failure #13 --- Thank you very much for your help. (*)-------------------------------------------------------- from twisted.internet import protocol, reactor, defer, utils from twisted.protocols import basic class FingerProtocol(basic.LineReceiver): def lineReceived(self, user): self.factory.getUser(user ).addCallback(lambda m: (self.transport.write(m+"\r\n\n")) ).addErrback(lambda x : x.printDetailedTraceback()) class FingerFactory(protocol.ServerFactory): protocol = FingerProtocol def getUser(self, user): return utils.getProcessOutput("ls",["-l -a"]) reactor.listenTCP(1079, FingerFactory()) reactor.run() -- Cordialement Philippe
On Tue, 2004-09-14 at 13:58 +0200, Philippe Bouige wrote:
Hello,
But when i change by : return utils.getProcessOutput("ls",["-l *"]) or : return utils.getProcessOutput("ls",["-l -a"]) or : return utils.getProcessOutput("ls",["-l ."])
and i have allways this error, but I don't undestand ??
Philippe, Twisted's getProcessOutput doesn't invoke your operating system's shell to run commands. When your shell sees "ls -l *" in a directory with the files "file1 file2 file3", it converts it (through a series of manipulations described in 'man 1 sh' on your platform) to ['/bin/ls', '-l', 'file1', 'file2', 'file3']. If you are really sure that is what you want, you can run getProcessOutput like this: utils.getProcessOutput("/bin/sh", ["-c", "ls -l *"]) but I recommend you do some reading up on how your operating system processes commands first.
On Tue, Sep 14, 2004 at 01:58:37PM +0200, Philippe Bouige wrote: [...]
But when i change by : return utils.getProcessOutput("ls",["-l *"]) or : return utils.getProcessOutput("ls",["-l -a"]) or : return utils.getProcessOutput("ls",["-l ."])
and i have allways this error, but I don't undestand ??
It's because you need to pass the paramater list that's passed to exec(2), not a command string that's passed to sh(1). At the kernel (i.e. syscall) level, the exec syscall receives a list of strings, where each argument in argv is a string, and can contain spaces or * or anything character, except '\0'. At the level you're probably used to, the shell, you just type "ls -ld *" or whatever, and the shell expands and splits that up into the list of strings for the exec syscall (in this example, something like ["ls", "-ld", "file1", "file2", ...]). If you want to do the shell's expansion and splitting of a commandline, just explicitly invoke then shell: return utils.getProcessOutput('sh', ['-c "ls -l *"']) (But beware passing user input directly into that; it's potentially a big security hole that will allow running of arbitrary shell commands) -Andrew.
On Tue, Sep 14, 2004 at 01:31:36PM +0100, Andrew Bennetts wrote: [...]
If you want to do the shell's expansion and splitting of a commandline, just explicitly invoke then shell:
return utils.getProcessOutput('sh', ['-c "ls -l *"'])
Gah, I of course meant: return utils.getProcessOutput('sh', ['-c', 'ls -l *']) -Andrew.
participants (3)
-
Andrew Bennetts
-
Glyph Lefkowitz
-
Philippe Bouige