Python urllib.splituser() Examples

The following are 30 code examples of urllib.splituser(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module urllib , or try the search function .
Example #1
Source File: user.py    From BitTorrent with GNU General Public License v3.0 5 votes vote down vote up
def uri_decode_idna(uri):
    '''
    Do IDNA decoding for hostnames, if possible
    '''
    scheme, netloc, path, query, fragment  = urlparse.urlsplit(uri)
    if scheme.lower() in urlparse.uses_netloc and netloc is not None:
        user_password, host_port = urllib.splituser(netloc)
        if host_port is not None:
            host, port = urllib.splitport(host_port)
            if host is not None and host[:1] + host[-1:] != '[]':
                # NOTE: this works around a bug in the urlparse cache w.r.t. unicode strings
                host = ''.join([ chr(ord(ch)) for ch in host ])
                try:
                    host = urllib.quote(decode_idna(urllib.unquote(host)))
                except:
                    pass
                try:
                    host = urllib.quote(unicodedata.normalize('NFKC', urllib.unquote(host).decode('utf-8')).encode('utf-8'))
                except:
                    pass
                host_port = host + (port is not None and (':' + port) or '')
                netloc = (user_password is not None and (user_password + '@') or '') + host_port
        pass
    uri = urlparse.urlunsplit((scheme, netloc, path, query, fragment))
    # NOTE: this works around a bug in the urlparse cache w.r.t. unicode strings
    uri = ''.join([ chr(ord(ch)) for ch in uri ])
    return uri 
Example #2
Source File: xmlrpclib.py    From CTFCrackTools with GNU General Public License v3.0 5 votes vote down vote up
def get_host_info(self, host):

        x509 = {}
        if isinstance(host, TupleType):
            host, x509 = host

        import urllib
        auth, host = urllib.splituser(host)

        if auth:
            import base64
            auth = base64.encodestring(urllib.unquote(auth))
            auth = string.join(string.split(auth), "") # get rid of whitespace
            extra_headers = [
                ("Authorization", "Basic " + auth)
                ]
        else:
            extra_headers = None

        return host, extra_headers, x509

    ##
    # Connect to server.
    #
    # @param host Target host.
    # @return A connection handle. 
Example #3
Source File: test_urllib.py    From CTFCrackTools with GNU General Public License v3.0 5 votes vote down vote up
def test_splituser(self):
        splituser = urllib.splituser
        self.assertEqual(splituser('User:Pass@www.python.org:080'),
                         ('User:Pass', 'www.python.org:080'))
        self.assertEqual(splituser('@www.python.org:080'),
                         ('', 'www.python.org:080'))
        self.assertEqual(splituser('www.python.org:080'),
                         (None, 'www.python.org:080'))
        self.assertEqual(splituser('User:Pass@'),
                         ('User:Pass', ''))
        self.assertEqual(splituser('User@example.com:Pass@www.python.org:080'),
                         ('User@example.com:Pass', 'www.python.org:080')) 
Example #4
Source File: xmlrpclib.py    From CTFCrackTools with GNU General Public License v3.0 5 votes vote down vote up
def get_host_info(self, host):

        x509 = {}
        if isinstance(host, TupleType):
            host, x509 = host

        import urllib
        auth, host = urllib.splituser(host)

        if auth:
            import base64
            auth = base64.encodestring(urllib.unquote(auth))
            auth = string.join(string.split(auth), "") # get rid of whitespace
            extra_headers = [
                ("Authorization", "Basic " + auth)
                ]
        else:
            extra_headers = None

        return host, extra_headers, x509

    ##
    # Connect to server.
    #
    # @param host Target host.
    # @return A connection handle. 
Example #5
Source File: xmlrpclib.py    From canape with GNU General Public License v3.0 5 votes vote down vote up
def get_host_info(self, host):

        x509 = {}
        if isinstance(host, TupleType):
            host, x509 = host

        import urllib
        auth, host = urllib.splituser(host)

        if auth:
            import base64
            auth = base64.encodestring(urllib.unquote(auth))
            auth = string.join(string.split(auth), "") # get rid of whitespace
            extra_headers = [
                ("Authorization", "Basic " + auth)
                ]
        else:
            extra_headers = None

        return host, extra_headers, x509

    ##
    # Connect to server.
    #
    # @param host Target host.
    # @return A connection handle. 
Example #6
Source File: xmlrpclib.py    From unity-python with MIT License 5 votes vote down vote up
def get_host_info(self, host):

        x509 = {}
        if isinstance(host, TupleType):
            host, x509 = host

        import urllib
        auth, host = urllib.splituser(host)

        if auth:
            import base64
            auth = base64.encodestring(urllib.unquote(auth))
            auth = string.join(string.split(auth), "") # get rid of whitespace
            extra_headers = [
                ("Authorization", "Basic " + auth)
                ]
        else:
            extra_headers = None

        return host, extra_headers, x509

    ##
    # Connect to server.
    #
    # @param host Target host.
    # @return A connection handle. 
Example #7
Source File: xmlrpclib.py    From PokemonGo-DesktopMap with MIT License 5 votes vote down vote up
def get_host_info(self, host):

        x509 = {}
        if isinstance(host, TupleType):
            host, x509 = host

        import urllib
        auth, host = urllib.splituser(host)

        if auth:
            import base64
            auth = base64.encodestring(urllib.unquote(auth))
            auth = string.join(string.split(auth), "") # get rid of whitespace
            extra_headers = [
                ("Authorization", "Basic " + auth)
                ]
        else:
            extra_headers = None

        return host, extra_headers, x509

    ##
    # Connect to server.
    #
    # @param host Target host.
    # @return A connection handle. 
Example #8
Source File: xmlrpclib.py    From RevitBatchProcessor with GNU General Public License v3.0 5 votes vote down vote up
def get_host_info(self, host):

        x509 = {}
        if isinstance(host, TupleType):
            host, x509 = host

        import urllib
        auth, host = urllib.splituser(host)

        if auth:
            import base64
            auth = base64.encodestring(urllib.unquote(auth))
            auth = string.join(string.split(auth), "") # get rid of whitespace
            extra_headers = [
                ("Authorization", "Basic " + auth)
                ]
        else:
            extra_headers = None

        return host, extra_headers, x509

    ##
    # Connect to server.
    #
    # @param host Target host.
    # @return A connection handle. 
Example #9
Source File: xmlrpclib.py    From CTFCrackTools-V2 with GNU General Public License v3.0 5 votes vote down vote up
def get_host_info(self, host):

        x509 = {}
        if isinstance(host, TupleType):
            host, x509 = host

        import urllib
        auth, host = urllib.splituser(host)

        if auth:
            import base64
            auth = base64.encodestring(urllib.unquote(auth))
            auth = string.join(string.split(auth), "") # get rid of whitespace
            extra_headers = [
                ("Authorization", "Basic " + auth)
                ]
        else:
            extra_headers = None

        return host, extra_headers, x509

    ##
    # Connect to server.
    #
    # @param host Target host.
    # @return A connection handle. 
Example #10
Source File: test_urllib.py    From CTFCrackTools-V2 with GNU General Public License v3.0 5 votes vote down vote up
def test_splituser(self):
        splituser = urllib.splituser
        self.assertEqual(splituser('User:Pass@www.python.org:080'),
                         ('User:Pass', 'www.python.org:080'))
        self.assertEqual(splituser('@www.python.org:080'),
                         ('', 'www.python.org:080'))
        self.assertEqual(splituser('www.python.org:080'),
                         (None, 'www.python.org:080'))
        self.assertEqual(splituser('User:Pass@'),
                         ('User:Pass', ''))
        self.assertEqual(splituser('User@example.com:Pass@www.python.org:080'),
                         ('User@example.com:Pass', 'www.python.org:080')) 
Example #11
Source File: xmlrpclib.py    From CTFCrackTools-V2 with GNU General Public License v3.0 5 votes vote down vote up
def get_host_info(self, host):

        x509 = {}
        if isinstance(host, TupleType):
            host, x509 = host

        import urllib
        auth, host = urllib.splituser(host)

        if auth:
            import base64
            auth = base64.encodestring(urllib.unquote(auth))
            auth = string.join(string.split(auth), "") # get rid of whitespace
            extra_headers = [
                ("Authorization", "Basic " + auth)
                ]
        else:
            extra_headers = None

        return host, extra_headers, x509

    ##
    # Connect to server.
    #
    # @param host Target host.
    # @return A connection handle. 
Example #12
Source File: _pydev_xmlrpclib.py    From filmkodi with Apache License 2.0 5 votes vote down vote up
def get_host_info(self, host):

        x509 = {}
        if isinstance(host, TupleType):
            host, x509 = host

        import urllib
        auth, host = urllib.splituser(host)

        if auth:
            import base64
            auth = base64.encodestring(urllib.unquote(auth))
            auth = string.join(string.split(auth), "") # get rid of whitespace
            extra_headers = [
                ("Authorization", "Basic " + auth)
                ]
        else:
            extra_headers = None

        return host, extra_headers, x509

    ##
    # Connect to server.
    #
    # @param host Target host.
    # @return A connection handle. 
Example #13
Source File: util.py    From conary with Apache License 2.0 5 votes vote down vote up
def urlSplit(url, defaultPort = None):
    """A function to split a URL in the format
    <scheme>://<user>:<pass>@<host>:<port>/<path>;<params>#<fragment>
    into a tuple
    (<scheme>, <user>, <pass>, <host>, <port>, <path>, <params>, <fragment>)
    Any missing pieces (user/pass) will be set to None.
    If the port is missing, it will be set to defaultPort; otherwise, the port
    should be a numeric value.
    """
    scheme, netloc, path, query, fragment = urlparse.urlsplit(url)
    userpass, hostport = urllib.splituser(netloc)
    if scheme == 'lookaside':
        # Always a local path, sometimes the first part will have a colon in it
        # but it isn't a port, e.g. "lp:lightdm".
        host, port = hostport, None
    else:
        host, port = networking.splitHostPort(hostport)
    if port is None:
        port = defaultPort

    if userpass:
        user, passwd = urllib.splitpasswd(userpass)
        if sys.version_info[:2] == (2, 7):
            # splituser is considered internal and changed
            # behavior in 2.7.  New behavior is right because
            # it allows : in password, but we must deal with
            # the old 2.6 behavior and not double-unquote
            user = urllib.unquote(user)
            if passwd:
                passwd = urllib.unquote(passwd)
        if passwd:
            passwd = ProtectedString(passwd)
    else:
        user, passwd = None, None
    return scheme, user, passwd, host, port, path, \
        query or None, fragment or None 
Example #14
Source File: util.py    From conary with Apache License 2.0 5 votes vote down vote up
def stripUserPassFromUrl(url):
    arr = list(urlparse.urlparse(url))
    hostUserPass = arr[1]
    userPass, host = urllib.splituser(hostUserPass)
    arr[1] = host
    return urlparse.urlunparse(arr) 
Example #15
Source File: metadata.py    From BitTorrent with GNU General Public License v3.0 5 votes vote down vote up
def normalize_uri(uri):
    '''
    normalize a URI; also converts IRIs to URIs
    '''
    uri = ''.join([ (ord(x) in xrange(33, 127)) and x or urllib.quote(x, safe='') for x in u''.join(uri.decode('UTF-8').split()).encode('UTF-8') ])
    try:
        scheme, netloc, path, query, fragment = urlparse.urlsplit(uri)
        uri = urlparse.urlunsplit((scheme, netloc, path, query, fragment))
        if scheme in urlparse.uses_netloc:
            if netloc is not None:
                user, hostport = urllib.splituser(netloc)
                if hostport is not None:
                    host, port = urllib.splitport(hostport)
                    if host is not None:
                        if host[:1] != '[':
                            # hostname segments get downcased and IDNA-encoded
                            ohost = []
                            for part in host.split('.'):
                                part = urllib.unquote(part)
                                try:
                                    part = part.decode('UTF-8').lower().encode('UTF-8')
                                    try:
                                        part = part.decode('UTF-8').encode('idna')
                                        pass
                                    except KeyboardInterrupt, k:
                                        raise
                                    except:
                                        pass
                                    pass
                                except KeyboardInterrupt, k:
                                    raise
                                except: 
Example #16
Source File: xmlrpclib.py    From meddle with MIT License 5 votes vote down vote up
def get_host_info(self, host):

        x509 = {}
        if isinstance(host, TupleType):
            host, x509 = host

        import urllib
        auth, host = urllib.splituser(host)

        if auth:
            import base64
            auth = base64.encodestring(urllib.unquote(auth))
            auth = string.join(string.split(auth), "") # get rid of whitespace
            extra_headers = [
                ("Authorization", "Basic " + auth)
                ]
        else:
            extra_headers = None

        return host, extra_headers, x509

    ##
    # Connect to server.
    #
    # @param host Target host.
    # @return A connection handle. 
Example #17
Source File: user.py    From BitTorrent with GNU General Public License v3.0 5 votes vote down vote up
def uri_encode_idna(uri):
    '''
    Do IDNA encoding for hostnames, if possible
    '''
    scheme, netloc, path, query, fragment  = urlparse.urlsplit(uri)
    if scheme.lower() in urlparse.uses_netloc and netloc is not None:
        user_password, host_port = urllib.splituser(netloc)
        if host_port is not None:
            host, port = urllib.splitport(host_port)
            if host is not None and host[:1] + host[-1:] != '[]':
                # NOTE: this works around a bug in the urlparse cache w.r.t. unicode strings
                host = ''.join([ chr(ord(ch)) for ch in host ])
                try:
                    host = urllib.quote(unicodedata.normalize('NFKC', urllib.unquote(host).decode('utf-8')).encode('utf-8'))
                except:
                    pass
                try:
                    host = urllib.quote(encode_idna(urllib.unquote(host)))
                except:
                    pass
                host_port = host + (port is not None and (':' + port) or '')
                netloc = (user_password is not None and (user_password + '@') or '') + host_port
        pass
    uri = urlparse.urlunsplit((scheme, netloc, path, query, fragment))
    # NOTE: this works around a bug in the urlparse cache w.r.t. unicode strings
    uri = ''.join([ chr(ord(ch)) for ch in uri ])
    return uri 
Example #18
Source File: xmlrpclib.py    From medicare-demo with Apache License 2.0 5 votes vote down vote up
def get_host_info(self, host):

        x509 = {}
        if isinstance(host, TupleType):
            host, x509 = host

        import urllib
        auth, host = urllib.splituser(host)

        if auth:
            import base64
            auth = base64.encodestring(urllib.unquote(auth))
            auth = string.join(string.split(auth), "") # get rid of whitespace
            extra_headers = [
                ("Authorization", "Basic " + auth)
                ]
        else:
            extra_headers = None

        return host, extra_headers, x509

    ##
    # Connect to server.
    #
    # @param host Target host.
    # @return A connection handle. 
Example #19
Source File: Client.py    From p2pool-n with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, url, config = Config):
        proto, uri = urllib.splittype(url)

        # apply some defaults
        if uri[0:2] != '//':
            if proto != None:
                uri = proto + ':' + uri

            uri = '//' + uri
            proto = 'http'

        host, path = urllib.splithost(uri)

        try:
            int(host)
            host = 'localhost:' + host
        except:
            pass

        if not path:
            path = '/'

        if proto not in ('http', 'https', 'httpg'):
            raise IOError, "unsupported SOAP protocol"
        if proto == 'httpg' and not config.GSIclient:
            raise AttributeError, \
                  "GSI client not supported by this Python installation"
        if proto == 'https' and not config.SSLclient:
            raise AttributeError, \
                "SSL client not supported by this Python installation"

        self.user,host = urllib.splituser(host)
        self.proto = proto
        self.host = host
        self.path = path 
Example #20
Source File: xmlrpclib.py    From Splunking-Crime with GNU Affero General Public License v3.0 5 votes vote down vote up
def get_host_info(self, host):

        x509 = {}
        if isinstance(host, TupleType):
            host, x509 = host

        import urllib
        auth, host = urllib.splituser(host)

        if auth:
            import base64
            auth = base64.encodestring(urllib.unquote(auth))
            auth = string.join(string.split(auth), "") # get rid of whitespace
            extra_headers = [
                ("Authorization", "Basic " + auth)
                ]
        else:
            extra_headers = None

        return host, extra_headers, x509

    ##
    # Connect to server.
    #
    # @param host Target host.
    # @return A connection handle. 
Example #21
Source File: websucker.py    From datafari with Apache License 2.0 5 votes vote down vote up
def savefilename(self, url):
        type, rest = urllib.splittype(url)
        host, path = urllib.splithost(rest)
        path = path.lstrip("/")
        user, host = urllib.splituser(host)
        host, port = urllib.splitnport(host)
        host = host.lower()
        if not path or path[-1] == "/":
            path = path + "index.html"
        if os.sep != "/":
            path = os.sep.join(path.split("/"))
        path = os.path.join(host, path)
        return path 
Example #22
Source File: protocol.py    From gglsbl with Apache License 2.0 5 votes vote down vote up
def url_permutations(url):
        """Try all permutations of hostname and path which can be applied

        to blacklisted URLs
        """
        def url_host_permutations(host):
            if re.match(r'\d+\.\d+\.\d+\.\d+', host):
                yield host
                return
            parts = host.split('.')
            l = min(len(parts), 5)
            if l > 4:
                yield host
            for i in range(l - 1):
                yield '.'.join(parts[i - l:])

        def url_path_permutations(path):
            yield path
            query = None
            if '?' in path:
                path, query = path.split('?', 1)
            if query is not None:
                yield path
            path_parts = path.split('/')[0:-1]
            curr_path = ''
            for i in range(min(4, len(path_parts))):
                curr_path = curr_path + path_parts[i] + '/'
                yield curr_path

        protocol, address_str = urllib.splittype(url)
        host, path = urllib.splithost(address_str)
        user, host = urllib.splituser(host)
        host, port = urllib.splitport(host)
        host = host.strip('/')
        seen_permutations = set()
        for h in url_host_permutations(host):
            for p in url_path_permutations(path):
                u = '{}{}'.format(h, p)
                if u not in seen_permutations:
                    yield u
                    seen_permutations.add(u) 
Example #23
Source File: xmlrpclib.py    From pmatic with GNU General Public License v2.0 5 votes vote down vote up
def get_host_info(self, host):

        x509 = {}
        if isinstance(host, TupleType):
            host, x509 = host

        import urllib
        auth, host = urllib.splituser(host)

        if auth:
            import base64
            auth = base64.encodestring(urllib.unquote(auth))
            auth = string.join(string.split(auth), "") # get rid of whitespace
            extra_headers = [
                ("Authorization", "Basic " + auth)
                ]
        else:
            extra_headers = None

        return host, extra_headers, x509

    ##
    # Connect to server.
    #
    # @param host Target host.
    # @return A connection handle. 
Example #24
Source File: _pydev_xmlrpclib.py    From PyDev.Debugger with Eclipse Public License 1.0 5 votes vote down vote up
def get_host_info(self, host):

        x509 = {}
        if isinstance(host, TupleType):
            host, x509 = host

        import urllib
        auth, host = urllib.splituser(host)

        if auth:
            import base64
            auth = base64.encodestring(urllib.unquote(auth))
            auth = string.join(string.split(auth), "") # get rid of whitespace
            extra_headers = [
                ("Authorization", "Basic " + auth)
                ]
        else:
            extra_headers = None

        return host, extra_headers, x509

    ##
    # Connect to server.
    #
    # @param host Target host.
    # @return A connection handle. 
Example #25
Source File: xmlrpclib.py    From oss-ftp with MIT License 5 votes vote down vote up
def get_host_info(self, host):

        x509 = {}
        if isinstance(host, TupleType):
            host, x509 = host

        import urllib
        auth, host = urllib.splituser(host)

        if auth:
            import base64
            auth = base64.encodestring(urllib.unquote(auth))
            auth = string.join(string.split(auth), "") # get rid of whitespace
            extra_headers = [
                ("Authorization", "Basic " + auth)
                ]
        else:
            extra_headers = None

        return host, extra_headers, x509

    ##
    # Connect to server.
    #
    # @param host Target host.
    # @return A connection handle. 
Example #26
Source File: websucker.py    From oss-ftp with MIT License 5 votes vote down vote up
def savefilename(self, url):
        type, rest = urllib.splittype(url)
        host, path = urllib.splithost(rest)
        path = path.lstrip("/")
        user, host = urllib.splituser(host)
        host, port = urllib.splitnport(host)
        host = host.lower()
        if not path or path[-1] == "/":
            path = path + "index.html"
        if os.sep != "/":
            path = os.sep.join(path.split("/"))
        path = os.path.join(host, path)
        return path 
Example #27
Source File: xmlrpclib.py    From BinderFilter with MIT License 5 votes vote down vote up
def get_host_info(self, host):

        x509 = {}
        if isinstance(host, TupleType):
            host, x509 = host

        import urllib
        auth, host = urllib.splituser(host)

        if auth:
            import base64
            auth = base64.encodestring(urllib.unquote(auth))
            auth = string.join(string.split(auth), "") # get rid of whitespace
            extra_headers = [
                ("Authorization", "Basic " + auth)
                ]
        else:
            extra_headers = None

        return host, extra_headers, x509

    ##
    # Connect to server.
    #
    # @param host Target host.
    # @return A connection handle. 
Example #28
Source File: xmlrpclib.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
def get_host_info(self, host):

        x509 = {}
        if isinstance(host, TupleType):
            host, x509 = host

        import urllib
        auth, host = urllib.splituser(host)

        if auth:
            import base64
            auth = base64.encodestring(urllib.unquote(auth))
            auth = string.join(string.split(auth), "") # get rid of whitespace
            extra_headers = [
                ("Authorization", "Basic " + auth)
                ]
        else:
            extra_headers = None

        return host, extra_headers, x509

    ##
    # Connect to server.
    #
    # @param host Target host.
    # @return A connection handle. 
Example #29
Source File: test_urllib.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
def test_splituser(self):
        splituser = urllib.splituser
        self.assertEqual(splituser('User:Pass@www.python.org:080'),
                         ('User:Pass', 'www.python.org:080'))
        self.assertEqual(splituser('@www.python.org:080'),
                         ('', 'www.python.org:080'))
        self.assertEqual(splituser('www.python.org:080'),
                         (None, 'www.python.org:080'))
        self.assertEqual(splituser('User:Pass@'),
                         ('User:Pass', ''))
        self.assertEqual(splituser('User@example.com:Pass@www.python.org:080'),
                         ('User@example.com:Pass', 'www.python.org:080')) 
Example #30
Source File: upload.py    From training_results_v0.5 with Apache License 2.0 4 votes vote down vote up
def _GuessBase(self, required):
    """Returns the SVN base URL.

    Args:
      required: If true, exits if the url can't be guessed, otherwise None is
        returned.
    """
    info = RunShell(["svn", "info"])
    for line in info.splitlines():
      words = line.split()
      if len(words) == 2 and words[0] == "URL:":
        url = words[1]
        scheme, netloc, path, params, query, fragment = urlparse.urlparse(url)
        username, netloc = urllib.splituser(netloc)
        if username:
          logging.info("Removed username from base URL")
        if netloc.endswith("svn.python.org"):
          if netloc == "svn.python.org":
            if path.startswith("/projects/"):
              path = path[9:]
          elif netloc != "pythondev@svn.python.org":
            ErrorExit("Unrecognized Python URL: %s" % url)
          base = "http://svn.python.org/view/*checkout*%s/" % path
          logging.info("Guessed Python base = %s", base)
        elif netloc.endswith("svn.collab.net"):
          if path.startswith("/repos/"):
            path = path[6:]
          base = "http://svn.collab.net/viewvc/*checkout*%s/" % path
          logging.info("Guessed CollabNet base = %s", base)
        elif netloc.endswith(".googlecode.com"):
          path = path + "/"
          base = urlparse.urlunparse(("http", netloc, path, params,
                                      query, fragment))
          logging.info("Guessed Google Code base = %s", base)
        else:
          path = path + "/"
          base = urlparse.urlunparse((scheme, netloc, path, params,
                                      query, fragment))
          logging.info("Guessed base = %s", base)
        return base
    if required:
      ErrorExit("Can't find URL in output from svn info")
    return None