diff --git a/core/__init__.py b/core/__init__.py index 9f9fe812..46eef2c3 100644 --- a/core/__init__.py +++ b/core/__init__.py @@ -1,4 +1,6 @@ # coding=utf-8 + +from six.moves import reload_module import locale import os import re @@ -252,7 +254,7 @@ def initialize(section=None): SYS_ENCODING = 'UTF-8' if not hasattr(sys, "setdefaultencoding"): - reload(sys) + reload_module(sys) try: # pylint: disable=E1101 diff --git a/core/transmissionrpc/client.py b/core/transmissionrpc/client.py index c451e685..b64b709d 100644 --- a/core/transmissionrpc/client.py +++ b/core/transmissionrpc/client.py @@ -18,12 +18,8 @@ from core.transmissionrpc.torrent import Torrent from core.transmissionrpc.session import Session from six import PY3, integer_types, string_types, iteritems -if PY3: - from urllib.parse import urlparse - from urllib.request import urlopen -else: - from urlparse import urlparse - from urllib2 import urlopen +from six.moves.urllib_parse import urlparse +from six.moves.urllib_request import urlopen def debug_httperror(error): diff --git a/core/transmissionrpc/httphandler.py b/core/transmissionrpc/httphandler.py index 1e884399..2968762e 100644 --- a/core/transmissionrpc/httphandler.py +++ b/core/transmissionrpc/httphandler.py @@ -4,19 +4,15 @@ import sys -from core.transmissionrpc.error import HTTPHandlerError -from six import PY3 +from six.moves.urllib_request import ( + build_opener, install_opener, + HTTPBasicAuthHandler, HTTPDigestAuthHandler, HTTPPasswordMgrWithDefaultRealm, + Request, +) +from six.moves.urllib_error import HTTPError, URLError +from six.moves.http_client import BadStatusLine -if PY3: - from urllib.request import Request, build_opener, \ - HTTPPasswordMgrWithDefaultRealm, HTTPBasicAuthHandler, HTTPDigestAuthHandler - from urllib.error import HTTPError, URLError - from http.client import BadStatusLine -else: - from urllib2 import Request, build_opener, \ - HTTPPasswordMgrWithDefaultRealm, HTTPBasicAuthHandler, HTTPDigestAuthHandler - from urllib2 import HTTPError, URLError - from httplib import BadStatusLine +from core.transmissionrpc.error import HTTPHandlerError class HTTPHandler(object): diff --git a/core/utorrent/client.py b/core/utorrent/client.py index f8989acf..f8ddc80d 100644 --- a/core/utorrent/client.py +++ b/core/utorrent/client.py @@ -1,12 +1,17 @@ # coding=utf8 -import urllib -import urllib2 -import urlparse -import cookielib import json import re -import StringIO + +from six import StringIO +from six.moves.http_cookiejar import CookieJar +from six.moves.urllib_error import HTTPError +from six.moves.urllib_parse import urljoin, urlencode +from six.moves.urllib_request import ( + build_opener, install_opener, + HTTPBasicAuthHandler, HTTPCookieProcessor, + Request, +) from core.utorrent.upload import MultiPartForm @@ -23,23 +28,23 @@ class UTorrentClient(object): def _make_opener(self, realm, base_url, username, password): """uTorrent API need HTTP Basic Auth and cookie support for token verify.""" - auth_handler = urllib2.HTTPBasicAuthHandler() + auth_handler = HTTPBasicAuthHandler() auth_handler.add_password(realm=realm, uri=base_url, user=username, passwd=password) - opener = urllib2.build_opener(auth_handler) - urllib2.install_opener(opener) + opener = build_opener(auth_handler) + install_opener(opener) - cookie_jar = cookielib.CookieJar() - cookie_handler = urllib2.HTTPCookieProcessor(cookie_jar) + cookie_jar = CookieJar() + cookie_handler = HTTPCookieProcessor(cookie_jar) handlers = [auth_handler, cookie_handler] - opener = urllib2.build_opener(*handlers) + opener = build_opener(*handlers) return opener def _get_token(self): - url = urlparse.urljoin(self.base_url, 'token.html') + url = urljoin(self.base_url, 'token.html') response = self.opener.open(url) token_re = "
" match = re.search(token_re, response.read()) @@ -122,8 +127,8 @@ class UTorrentClient(object): def _action(self, params, body=None, content_type=None): # about token, see https://github.com/bittorrent/webui/wiki/TokenSystem - url = self.base_url + '?token=' + self.token + '&' + urllib.urlencode(params) - request = urllib2.Request(url) + url = self.base_url + '?token=' + self.token + '&' + urlencode(params) + request = Request(url) if body: request.add_data(body) @@ -134,5 +139,5 @@ class UTorrentClient(object): try: response = self.opener.open(request) return response.code, json.loads(response.read()) - except urllib2.HTTPError, e: + except HTTPError: raise