[Tutor] proxy switcher - was Re: I love python / you guys :)

Stephen Nelson-Smith sanelson at gmail.com
Mon Nov 16 15:30:42 CET 2009


Hi,

>> When i use our company's LAN i set my proxy variable by hand in .bashrc.
>> There are 4 files to insert proxy variable:
>>
>> in ~/.bashrc, /root/.bashrc, /etc/wgetrc and /etc/apt/apt.conf.
>>
>> The last one is actually rename e.g. mv to apt.conf to activate proxy and mv
>> to apt.conf.bak to deactivate. The proxy variable is something like this
>>
>> export http_proxy=http://username:password@proxy:port
>> ftp_proxy=$http_proxy
>>
>> To activate i uncomment them then source .bashrc. To deactivate i put back
>> the comment sign. I do it all in vim e.g. vim -o the-3-files-above. For
>> apt.conf see rename above. I deactivate because i have another internet
>> connection option via 3G usb modem. But thats another story.
>>
>> I will do this myself in python so please show me the way. Surely this can
>> be done.

Here's what I knocked up over lunch.  It doesn't cover the moving of
the file, I don't like that it's deep-nested, and I've not tested it,
but I welcome criticism and feedback:

files = ['file1', 'file2', 'file3', 'file4']
settings = ['export http_proxy=', 'ftp_proxy=']

for file in files:
  with open(file, 'rw') as file:
    for line in file:
      for setting in settings:
        if setting in line:
          if line[0] == '#':
            line = line[1:]
          else:
            line =  '#' + line
        output.write(line)

S.


More information about the Tutor mailing list