problem occurs with replaced values using fileinput

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Phoe6

    #1

    problem occurs with replaced values using fileinput

    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:8 0"

    proxy_user = 'senthil_or'
    proxy_password_ orig='password'
    proxy_password = urllib.quote(pr oxy_password_or ig,"")

    # Setup the Proxy with urllib2

    proxy_url = 'http://' + proxy_user + ':' + proxy_password + '@' +
    PROXY_IP
    #proxy_url = urllib.quote(pr oxy_url_add)
    proxy_support = urllib2.ProxyHa ndler({"http":p roxy_url})
    opener = urllib2.build_o pener(proxy_sup port,urllib2.HT TPHandler)
    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.re ad('ConfigFile. txt')
    settings = {}
    settings['PROXY_IP'] = configparser.ge t('PROXY','PROX Y_IP')
    settings['PROXY_PORT'] =
    configparser.ge t('PROXY','PROX Y_PORT').strip( )
    settings['PROXY_USER'] = configparser.ge t('PROXY','PROX Y_USER')
    settings['PROXY_PASSWORD '] =
    configparser.ge t('PROXY','PROX Y_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_PAS SWORD'
    proxy_password = urllib.quote(pr oxy_password_or ig,"")
    # Setup the Proxy with urllib2
    proxy_url = 'http://' + proxy_user + ':' + proxy_password + '@' +
    proxy_ip
    #proxy_url = urllib.quote(pr oxy_url_add)
    proxy_support = urllib2.ProxyHa ndler({"http":p roxy_url})
    opener = urllib2.build_o pener(proxy_sup port,urllib2.HT TPHandler)
    urllib2.install _opener(opener)

    # Replacing Functions:
    def replaceProxy(se ttings):
    '''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(se ttings) 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

Working...