title: Python : Use Popen and Communicate and get the stdout in a loop
body: <p>I want to execute bash command and get the output and I decided to use Popen and Communicate to do this. this job has to be done in a loop. The problem is that in the first round of loop every thing is OK but in the next loops I get Error when using communicate function. I know that communicate function can only be used once but I create a new subprocess each loop and therefore running communicate should be possible . 
  Here's the Code:</p>
  
  <pre><code>with open('ComSites2.txt') as file:
  for site in file:
      sf = site.split()
      temp = "http://wwww." + sf[0]
      command = "wget --spider -S '%s' 2&gt;&amp;1 | grep 'HTTP/' | awk '{print $2}'" %temp
      print command
      output = subprocess.Popen(command,stdout=subprocess.PIPE, shell=True)
      StatusCode = int(output.communicate()[0])
      if StatusCode == 200:
          print "page found successfully"
      elif StatusCode == 404:
          print "Page not found!!"
      else:
          print "no result got!!"
  </code></pre>
  
  <p>when I run this I get this output : </p>
  
  <pre><code>wget --spider -S 'http://wwww.ACAServices.com' 2&gt;&amp;1 | grep 'HTTP/' | awk '{print $2}'
  page found successfully
  wget --spider -S 'http://wwww.ACI.com' 2&gt;&amp;1 | grep 'HTTP/' | awk '{print $2}'
  Traceback (most recent call last):
    File "/root/AghasiTestCases/URLConnector.py", line 17, in &lt;module&gt;
      StatusCode = int(output.communicate()[0])
  ValueError: invalid literal for int() with base 10: ''
  </code></pre>