Note that there are some explanatory texts on larger screens.

plurals
  1. POPython - pipelining subprocess in Windows
    text
    copied!<p>I'm using Windows 7, and I've tried this under Python 2.6.6 and Python 3.2.</p> <p>So I'm trying to call this command line from Python:</p> <pre><code>netstat -ano | find ":80" </code></pre> <p>under Windows cmd, this line works perfectly fine.</p> <p>So,</p> <ul> <li><p>1st attempt:</p> <pre><code>output = subprocess.Popen( [r'netstat -ano | find ":80"'], stdout=subprocess.PIPE, shell=True ).communicate() </code></pre> <p>An error is raised that 'find' actually didn't receive correct parameter (e.g. 'find ":80" \'):</p> <pre><code>Access denied - \ </code></pre></li> <li><p>2nd attempt:</p> <pre><code>#calling netstat cmd_netstat = subprocess.Popen( ['netstat','-ano'], stdout = subprocess.PIPE ) #pipelining netstat result into find cmd_find = subprocess.Popen( ['find','":80"'], stdin = cmd_netstat.stdout, stdout = subprocess.PIPE ) </code></pre> <p>Again, the same error is raised.</p> <pre><code>Access denied - \ </code></pre></li> </ul> <p>What did I do wrong? :(</p> <p>EDIT:</p> <ul> <li><p>3rd attempt (As @Pavel Repin suggested):</p> <pre><code>cmd_netstat = subprocess.Popen( ['cmd.exe', '-c', 'netstat -ano | find ":80"'], stdout=subprocess.PIPE ).communicate() </code></pre> <p>Unfortunately, subprocess with ['cmd.exe','-c'] results in something resembling deadlock or a blank cmd window. I assume '-c' is ignored by cmd, resulting in communicate() waiting indefinitely for cmd termination. Since this is Windows, my bet bet is cmd only accepts parameter starting with slash (/). So I substituted '-c' with '/c':</p> <pre><code>cmd_netstat = subprocess.Popen( ['cmd.exe', '/c', 'netstat -ano | find ":80"'], stdout=subprocess.PIPE ).communicate() </code></pre> <p>And...back to the same error:</p> <pre><code>Access denied - \ </code></pre></li> </ul> <p>EDIT: I gave up, I'll just process the string returned by 'netstat -ano' in Python. Might this be a bug?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload