problem occurs with replaced values using fileinput

Phoe6 orsenthil at gmail.com
Sat Dec 2 14:34:54 EST 2006


Hi All,

I am able to use urlib2 through proxy. I give proxy credentials and use


# Set the Proxy Address
proxy_ip = "10.0.1.1:80"

proxy_user = 'senthil_or'
proxy_password_orig='password'
proxy_password = urllib.quote(proxy_password_orig,"")

# Setup the Proxy with urllib2

proxy_url = 'http://' + proxy_user + ':' + proxy_password + '@' +
PROXY_IP
#proxy_url = urllib.quote(proxy_url_add)
proxy_support = urllib2.ProxyHandler({"http":proxy_url})
opener = urllib2.build_opener(proxy_support,urllib2.HTTPHandler)
urllib2.install_opener(opener)

--
Now, I have decided in my program, I shall give a Configfile.txt to
users who can enter the proxy details and I shall substitute the
details in the script;

#FILE: Configfile.txt
# Provide the Proxy Details
PROXY_IP: 10.0.1.1
PROXY_PORT: 80
PROXY_USER: senthil_or
PROXY_PASSWORD: password

#Config Parsing function.

def configParsed():
    """Parse the Configfile.txt"""
    configparser = ConfigParser()
    configparser.read('ConfigFile.txt')
    settings = {}
    settings['PROXY_IP'] = configparser.get('PROXY','PROXY_IP')
    settings['PROXY_PORT'] =
configparser.get('PROXY','PROXY_PORT').strip()
    settings['PROXY_USER'] = configparser.get('PROXY','PROXY_USER')
    settings['PROXY_PASSWORD'] =
configparser.get('PROXY','PROXY_PASSWORD')
    return settings

Now, in my script I do a replacement of values in the script:

FILE BEFORE Replacement:
# Set the Proxy Address
proxy_ip = "PROXY_IP:PROXY_PORT"

proxy_user = 'PROXY_USER'
proxy_password_orig='PROXY_PASSWORD'
proxy_password = urllib.quote(proxy_password_orig,"")
# Setup the Proxy with urllib2
proxy_url = 'http://' + proxy_user + ':' + proxy_password + '@' +
proxy_ip
#proxy_url = urllib.quote(proxy_url_add)
proxy_support = urllib2.ProxyHandler({"http":proxy_url})
opener = urllib2.build_opener(proxy_support,urllib2.HTTPHandler)
urllib2.install_opener(opener)

# Replacing Functions:
def replaceProxy(settings):
    '''Replace the Proxy Credential values'''
    for k,v in settings.items():
      for line in fileinput.input('src' + os.sep+'file.py',inplace=1):
        print line.replace(k,v),
    return


The problem, I am facing is, when I use a script to replace certain
variables with config file based values, the urllib2 is failing!! It is
not able to get the correct credentials to open the proxy!
I debuged and derived at the conclusion at there is something wrong
with replaceProxy(settings) function I am using:
1) Either with for k,v in settings.items()
2) OR with fileinput.input and print line.replace(k,v),

The output file with replaced values looks perfectly fine to human
eyes!  I dont know what is happening with automatic replacement of
values which is failing my program.

Has anyone faced this kind of scenario before?
Need your help in solving this.

Thanks,
Senthil




More information about the Python-list mailing list