On Mon, Nov 16, 2009 at 1:36 PM, bibi midi <bibsmen...@gmail.com> wrote: > > > On Mon, Nov 16, 2009 at 1:58 AM, Stefan Lesicnik <ste...@lsd.co.za> wrote: >> >> hi, >> >> Although not a question, i just want to tell you guys how awesome you are! >> >> I am not a programmer, i can do a bit of bash. I have never officially >> learnt programming, but numerous times looked at some perl, c, java >> and never really gotten past the beginning stages of it. That all >> changed when i picked up python. Although my style and use of python >> is probably barbaric at best, I really enjoy it when you can make >> things work. Python was just amazing from a readability / logical >> point of view. If i can think of something, there is a way to do it in >> python. After learning the simple data structures it seems i can >> really do anything i want to. > > > Hi Stefan, > > Your message mirrored my current state. There's TONS to learn in linux like > bash, perl, vim, sed, awk, python, etc. Too much to put in your head and in > the end makes you half-baked e.g. good at start but fades away in the end. > Anyway i hope to follow your lead and be one among the guys here > knowledgeable of the language. > > In the same lines of if-you-can-think-of-anything-python-can-do-it i got > inspired to ask a question for the gurus: > > 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:passw...@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. >
Heys, I do something similair with apt.conf. Below is an older version of an app i wrote (the new version i have written integrates into network manager, so when it detects a certain network, it switches all the corresponding settings!). This older version has some functions that you might be interested. Maybe something below helps you. #!/usr/bin/python import sys import fileinput import re import netifaces import glob import string from netaddr import IP, CIDR networks = {} networks["home"] = {"ip":"192.168.1.0/24","url":"http://192.168.1.2:3142/"} #networks["home"] = {"ip":"192.168.1.0/24","url":"http://ecstacy.lsd.co.za:3142/"} networks["office"] = {"ip":"172.30.16.16/20","url":"http://ecstacy.lsd.co.za:3142/"} #Things to keep in sync sync=["/etc/apt/sources.list","/etc/apt/sources.list.d/*.list"] #sync=["/etc/apt/sources.list"] #Search interfaces for IP and compare against variable list def netcheck(): for interface in netifaces.interfaces(): iface = netifaces.ifaddresses(interface) if netifaces.AF_INET in iface: networkaddr = '%s/%s' % (netifaces.ifaddresses(interface)[netifaces.AF_INET][0]['addr'], netifaces.ifaddresses(interface)[netifaces.AF_INET][0]['netmask']) networkaddr = IP(networkaddr).cidr() print 'networkaddr: %s' % networkaddr for name, values in networks.iteritems(): network = IP(values['ip']).cidr() print 'network: %s' % network if networkaddr in network: location = networks[name]['url'] print 'Location \'%s\' found. Setting proxy to \'%s\'' % (name,location) #Function to hash out mirrors based on regex - may be useful for enabling/disabling src package downloads #Not currently used def hash(): for line in fileinput.input("test.list", inplace=1): line = line.strip() if re.search('^deb', line): print '#%s' % line else: print line #Function to check if sync list actually exists (maybe should check for writable?) def checksync(): for file in sync: if not glob.glob(file): print ("Error: %s does not exist" %file) sys.exit(1) #Function to replace mirror def replace(location=False): for file in sync: for line in fileinput.input(glob.glob(file), inplace=1): line = line.strip() if location: if re.search('^deb.*http://(.*):3142/', line): print re.sub('http://(.*):3142/', location, line) elif re.search('^deb.*http://', line): print re.sub('http://', location, line) else: print line elif re.search('^deb.*http://(.*):3142/', line): print re.sub('http://(.*):3142/', 'http://', line) else: print line checksync() replace('http://ecstacy.lsd.co.za:3142/') #replace() _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor