def unhumanize_bandwidth(bitsstr):
    '''
    Take a string representing a link capacity, e.g., 10 Mb/s, and
    return an integer representing the number of bits/sec.
    Recognizes:
        - 'bits/sec' or 'b/s' are treated as plain bits per second
        - 'Kb' or 'kb' as thousand bits/sec
        - 'Mb' or 'mb' as million bits/sec
        - 'Gb' or 'gb' as billion bits/sec
        - 'Tb' or 'tb' as trillion bits/sec
        - if second character is 'B', quantity is interpreted as bytes/sec
        - any subsequent characters after the first two are ignored, so
          Kb/s Kb/sec Kbps are interpreted identically.

    Returns None if the string doesn't contain anything parseable.
    '''
    if isinstance(bitsstr, int):
        return bitsstr

    mobj = re.match('^\s*([\d\.]+)\s*(.*)\s*$', bitsstr)
    if not mobj:
        return None
    value, units = mobj.groups()
    value = float(value)
    multipliers = { 'b':1, 'k':1e3, 'm':1e6, 'g':1e9, 't':1e12 }
    if not units:
        units = 'bits'
    mult = multipliers.get(units[0].lower(), 0)
    bits = 1
    if len(units) > 1:
        if units[1] == 'B': bits = 8
    # print (bitsstr, value, mult, bits)
    return int(value * mult * bits)