In linux to set a proxy value you do the following.
proxy=http://$user:[email protected]:${NUM}
http_proxy="$proxy" https_proxy="$proxy" ${COMMAND}
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.
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.
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)
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. Subprocess module errors with 'export' in python on linux?
this is my implementation
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)
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?
POST-ANSWER EDIT: Interaction between Python script and linux shell
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.
envargument toPopen? Or are you actually trying to launch a shell and run scripts directly within it instead of in subshells, or something else funky?http_proxy,https_proxy, andbashCommand, it's hard to debug what might be going wrong. (Also, is there's a reason you're first doingcallon the command, and then creating aPopenwith the exact same command and callingcommunicateon it?)subprocess32backport to fix that), it's that some interactive programs require you to change the buffering, and others aren't talking to stdin at all (e.g., they're reading a password without echoing it and need a real tty, or they just ask libpam to do it for them, etc.) so piping to stdin doesn't do any good. And also that writing code to talk interactively over a pipe without blocking is hard.