1

I have a web2py application that is running the program "winexe" function through python subprocess.Popen. The problem arises when it is launched winexe: starts correctly but does not exit. Web2py runs on apache using mod_wsgi and with user www-data.

Code:

import os
import pwd
import base64

p = subprocess.Popen(['winexe', '--system', '-U user%password', '//ip_client', '"cmd /C wmic os get osarchitecture"'], stdout = subprocess.PIPE)

output = p.communicate()[0]

print output

if I run the same command from the command line with winexe working properly

winexe -U user%pass //ip_client "cmd /C wmic os get osarchitecture"

OSArchitecture
64 bit

Can you help me? Thanks

volly86
  • 23
  • 3
  • 1
    You're not running the same command, you're using `--system` for one.. Secondly somewhere in the back of my head i'm quite sure you're not allowed to execute system-commands from a wsgi script, but that's just something iv'e dreamt.. Can you add print/log-output between each line and see where it gets stuck? – Torxed Apr 09 '14 at 10:29
  • Personally, i'd try: `p = subprocess.Popen('winexe --system -U user%password //ip_client "cmd /C wmic os get osarchitecture"', stdout=subprocess.PIPE, shell=True)` – Torxed Apr 09 '14 at 10:30
  • I'm sorry, I forgot --system but the result is the same. I have tried also p = subprocess.Popen('winexe --system -U user%password //ip_client "cmd /C wmic os get osarchitecture"', stdout=subprocess.PIPE, shell=True) with the same result – volly86 Apr 09 '14 at 10:37
  • About WSGI... I also thought I, if so what solution do I have? – volly86 Apr 09 '14 at 10:41
  • As i mentioned, try `with open('debug.log', 'a') as fh:` and do `fh.write('I came here....')` between each line in your Web2py script. – Torxed Apr 09 '14 at 10:44
  • Also, https://code.google.com/p/modwsgi/wiki/DebuggingTechniques – Torxed Apr 09 '14 at 10:45
  • now I'll try ... but if I run any other command (eg ls -l) works correctly. I have the problem only with winexe. If I execute `ps aux | grep winexe`I see winexe running but doing nothing – volly86 Apr 09 '14 at 11:02

1 Answers1

0

For debugging porposes, use:

from subprocess import Popen, PIPE, STDOUT
with open('debug.log', 'a') as log:
    log.write('Starting subprocess\n')
    log.flush()
    handle = Popen('winexe --system -U user%password //ip_client "cmd /C wmic os get osarchitecture"', shell=True, stdout=PIPE, stderr=STDOUT, stdin=PIPE)
    log.write('Outputting everything that the subprocess does...\n')
    log.flush()
    while handle.poll() is None:
        log.write('Output: ' + str(handle.stdout.read()) + '\n')
        log.flush()
    log.write('Command ended with code: ' + str(handle.poll()) + '\n')
    log.flush()
    handle.stdout.close()
    handle.stdin.close()
Torxed
  • 22,866
  • 14
  • 82
  • 131
  • 1
    with you solution works perfectly there was some problem in reading output as above I solved that by changing!! thanks a lot! – volly86 Apr 09 '14 at 14:42