See the coding guidelines for correct space usage. Also, duplicating the port dict isn't DRY, so define it in a single spot. For example:

    default_ports = {'http': 80, 'https': 443}
    p1_port = p1.port or default_ports.get(p1.scheme, None)
    p2_port = p2.port or default_ports.get(p2.scheme, None)