title: how to set proxy value and run linux command in python
body: <p>In linux to set a proxy value you do the following.</p>
  
  <pre><code>proxy=http://$user:$password@proxy.server.com:${NUM}
  http_proxy="$proxy" https_proxy="$proxy" ${COMMAND}
  </code></pre>
  
  <p>For security reasons, if you run this in a subshell, you are not explicitly letting your password in the open, or in the logs. Problem with this approach is, I have to set user name and password everytime I want to run a command. </p>
  
  <p>Therefore decided to write a python code for it. I have a working version in C. Just wanted to learn more in Python. I have found nice ways to encode and decode my password, and after most of the hooplahs, I pass it to this function to test proxy connection. </p>
  
  <pre><code>def test_connection(creds,proxy_url):
      import pycurl
      import cStringIO
  
      buf = cStringIO.StringIO()
      test_url="http://www.google.com"
  
      c = pycurl.Curl()
      c.setopt(c.URL, test_url)
      c.setopt(c.WRITEFUNCTION, buf.write)
      c.setopt(c.PROXY, proxy_url)
      c.setopt(c.PROXYPORT, 8080)
      c.setopt(c.PROXYTYPE, c.PROXYTYPE_HTTP) 
      c.setopt(c.PROXYAUTH, c.HTTPAUTH_NTLM) 
      c.setopt(c.PROXYUSERPWD, creds) 
      c.perform()
      buf.close()
      return c.getinfo(c.RESPONSE_CODE)
  </code></pre>
  
  <p>Where I'm having problems is using suprocess, I do understand that subprocess does not allow you to use export, since it is not really a command. <a href="https://stackoverflow.com/questions/3929319/subprocess-module-errors-with-export-in-python-on-linux">Subprocess module errors with &#39;export&#39; in python on linux?</a> </p>
  
  <p>this is my implementation</p>
  
  <pre><code>finalCommand = ["/bin/sh", "-c"]
  finalCommand.append(http_proxy)
  finalCommand.append(https_proxy)
  for x in bashCommand:
      finalCommand.append(x)
  
  print subprocess.call(finalCommand)
  process = subprocess.Popen(finalCommand,stdout=subprocess.PIPE)
  
  
  out, err = process.communicate()
  
  print "Output ... \n %s" % (out)
  
  if err == None: 
      print "No errors"
  else: 
      print "Errors ... \n %s" %(err)
  </code></pre>
  
  <p>Unfortunately, after several tests, my program always return no output and no error. 
  I have printed the output of the curl, so i know the decode, encode, or proxy isn't the issue. Any suggestions?  </p>
  
  <p>POST-ANSWER EDIT: 
  <a href="https://stackoverflow.com/questions/22194774/interaction-between-python-script-and-linux-shell">Interaction between Python script and linux shell</a> </p>
  
  <p>env did solve my problem, but I also had to refer to the thread above. Some of the commands I ran were interactive one, and as it explains it well in the thread, PIPE doesn't work properly with interactive programs. </p>