problem occurs with replaced values using fileinput

P

Phoe6

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top