Package apt_p2p :: Module util
[hide private]
[frames] | no frames]

Module util

source code

Some utitlity functions for use in the apt-p2p program.

Classes [hide private]
  TestUtil
Tests for the utilities.
Functions [hide private]
 
findMyIPAddr(addrs, intended_port, local_ok=False)
Find the best IP address to use from a list of possibilities.
source code
 
ipAddrFromChicken()
Retrieve a possible IP address from the ipchecken website.
source code
(string, int)
uncompact(s)
Extract the contact info from a compact peer representation.
source code
string
compact(ip, port)
Create a compact representation of peer contact info.
source code
string
byte_format(s)
Format a byte size for reading by the user.
source code
Variables [hide private]
  isLocal = re.compile(r'^(192\.168\.[0-9]{1,3}\.[0-9]{1,3})|(10...
a compiled regular expression suitable for testing if an IP address is from a known local or private range

Imports: os, re, log, unittest


Function Details [hide private]

findMyIPAddr(addrs, intended_port, local_ok=False)

source code 

Find the best IP address to use from a list of possibilities.

Parameters:
  • addrs - the list of possible IP addresses
  • intended_port - the port that was supposed to be used
  • local_ok - whether known local/private IP ranges are allowed (defaults to False)
Returns:
the preferred IP address, or None if one couldn't be found

uncompact(s)

source code 

Extract the contact info from a compact peer representation.

Parameters:
  • s (string) - the compact representation
Returns: (string, int)
the IP address and port number to contact the peer on
Raises:
  • ValueError - if the compact representation doesn't exist

compact(ip, port)

source code 

Create a compact representation of peer contact info.

Parameters:
  • ip (string) - the IP address of the peer
  • port (int) - the port number to contact the peer on
Returns: string
the compact representation
Raises:
  • ValueError - if the compact representation doesn't exist

byte_format(s)

source code 

Format a byte size for reading by the user.

Parameters:
  • s (long) - the number of bytes
Returns: string
the formatted size with appropriate units

Variables Details [hide private]

isLocal

a compiled regular expression suitable for testing if an IP address is from a known local or private range
Value:
re.compile(r'^(192\.168\.[0-9]{1,3}\.[0-9]{1,3})|(10\.[0-9]{1,3}\.[0-9\
]{1,3}\.[0-9]{1,3})|(172\.0?1[6-9]\.[0-9]{1,3}\.[0-9]{1,3})|(172\.0?2[\
0-9]\.[0-9]{1,3}\.[0-9]{1,3})|(172\.0?3[0-1]\.[0-9]{1,3}\.[0-9]{1,3})|\
(127\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})$')