سؤال

I've used twisted to make an SSH server similar to the one shown here. I attempted to add curl functionality to it like so:

class CurlProcessProtocol(protocol.ProcessProtocol):
    def connectionMade(self):
        self.transport.closeStdin()

def do_curl(self, *args):
    "Sets up a download"
    curlProcess = CurlProcessProtocol()
    args = tuple(['curl'])+args
    reactor.spawnProcess(curlProcess, 'curl', args)

I have the files necessary for curl to run in the same directory as my program. When I connect to the SSH server and attempt a curl command, I get the following error: Error: (2, 'CreateProcess', 'The system cannot find the file specified.') I tried appending os.getcwd()+ before 'curl' to no avail.

هل كانت مفيدة؟

المحلول 2

Alright, so I needed to be more specific. Here is a working do_curl. I had to add a "\" after getcwd() and a ".exe" after curl.

def do_curl(self, *args):
    "Sets up a download"
    curlProcess = CurlProcessProtocol()
    args = tuple([os.getcwd()+'\curl.exe'])+args
    reactor.spawnProcess(curlProcess, os.getcwd()+'\curl.exe', args)

نصائح أخرى

Try using the actual path to the curl executable. Most likely it is not in os.getcwd(). /usr/bin is more likely (or possibly /bin or /usr/local/bin or some other system-defined location). You can probably find it by using which curl in a shell.

Or, add the PATH environment variable to your spawnProcess call -

reactor.spawnProcess(
    curlProcess, 'curl', args, env={b"PATH": os.environ[b"PATH"]})

Or, probably even better, add your entire environment:

reactor.spawnProcess(
    curlProcess, 'curl', args, env=os.environ)

PATH controls where the system looks for relative path names like "curl" when trying to execute them. If it is unset, you have to specify an absolute path to be able to execute anything.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top