#! /usr/bin/env python
-from cgi import parse_qsl
+try:
+ from urllib.parse import parse_qsl, urlencode, \
+ quote, unquote, unquote_plus, \
+ splittype, splithost, splituser, splitpasswd, \
+ splitport, splittag, splitquery
+except ImportError:
+ from cgi import parse_qsl
+ from urllib import urlencode, quote, unquote, unquote_plus, \
+ splittype, splithost, splituser, splitpasswd, \
+ splitport, splittag, splitquery
from getopt import getopt, GetoptError
-import sys, urllib
+import sys
from m_lib.defenc import default_encoding
# This must be imported and called before webbrowser
except GetoptError:
usage()
-if len(arguments) <> 1:
+if len(arguments) != 1:
usage()
encoding = None
encoding = default_encoding
url = arguments[0]
-protocol, request = urllib.splittype(url)
+protocol, request = splittype(url)
user, password, port = None, None, None
-host, path = urllib.splithost(request)
+host, path = splithost(request)
if host:
- user, host = urllib.splituser(host)
+ user, host = splituser(host)
if user:
- user, password = urllib.splitpasswd(user)
- host, port = urllib.splitport(host)
+ user, password = splitpasswd(user)
+ host, port = splitport(host)
if port: port = int(port)
-path, tag = urllib.splittag(path)
-path, query = urllib.splitquery(path)
-path = urllib.unquote(path)
-if tag: tag = urllib.unquote_plus(tag)
+path, tag = splittag(path)
+path, query = splitquery(path)
+path = unquote(path)
+if tag: tag = unquote_plus(tag)
if query:
qlist = []
url = protocol + "://"
if user:
- url += urllib.quote(unicode(user, default_encoding).encode(encoding))
+ url += quote(unicode(user, default_encoding).encode(encoding))
if password:
- url += ':' + urllib.quote(unicode(password, default_encoding).encode(encoding))
+ url += ':' + quote(unicode(password, default_encoding).encode(encoding))
url += '@'
if host:
url += host.decode(encoding).encode('idna')
url += ':%d' % port
if path:
if protocol == "file":
- url += urllib.quote(path)
+ url += quote(path)
else:
- url += urllib.quote(unicode(path, default_encoding).encode(encoding))
+ url += quote(unicode(path, default_encoding).encode(encoding))
if query:
- url += '?' + urllib.urlencode(qlist)
+ url += '?' + urlencode(qlist)
if tag:
- url += '#' + urllib.quote_plus(unicode(tag, default_encoding).encode(encoding))
+ url += '#' + quote_plus(unicode(tag, default_encoding).encode(encoding))
webbrowser.open(url, new)