From 727a8eac99ee8f8ff6db7de893ab10b24984f316 Mon Sep 17 00:00:00 2001 From: Dong Gang Date: Fri, 24 Aug 2018 10:20:55 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9urllib=E7=9B=B8=E5=85=B3?= =?UTF-8?q?=E5=BA=93=EF=BC=8C=E6=94=AF=E6=8C=81python3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dce_plugin/__init__.py | 12 ++++++------ dce_plugin/docker_client.py | 16 ++++++++-------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/dce_plugin/__init__.py b/dce_plugin/__init__.py index 32d8891..bfa7654 100644 --- a/dce_plugin/__init__.py +++ b/dce_plugin/__init__.py @@ -4,8 +4,8 @@ import json import os import ssl -import urllib2 -import urlparse +import urllib.request +import urllib.parse from .docker_client import DockerClient @@ -93,13 +93,13 @@ def _plugin_storage_url(self): return storage_url.format(**config) def _build_request(self, method, url, data=None): - parts = urlparse.urlparse(url) + parts = urllib.parse.urllib.parse(url) safe_parts = parts._replace(netloc='{}:{}'.format(parts.hostname, parts.port)) safe_url = safe_parts.geturl() auth = base64.urlsafe_b64encode('{}:{}'.format(parts.username, parts.password)) - req = urllib2.Request(safe_url, data=data) + req = urllib.request.Request(safe_url, data=data) req.add_header('Authorization', 'Basic {}'.format(auth)) req.add_header('Content-Type', 'application/json') req.get_method = lambda: method @@ -111,7 +111,7 @@ def set_config(self, config): raise PluginSDKException("config should not bigger than 1MB") storage_url = self._plugin_storage_url() - response = urllib2.urlopen( + response = urllib.request.urlopen( self._build_request('PUT', storage_url, data), context=ssl._create_unverified_context() ) @@ -119,7 +119,7 @@ def set_config(self, config): def get_config(self): storage_url = self._plugin_storage_url() - response = urllib2.urlopen( + response = urllib.request.urlopen( self._build_request('GET', storage_url), context=ssl._create_unverified_context() ) diff --git a/dce_plugin/docker_client.py b/dce_plugin/docker_client.py index e85dbaa..2ba7777 100644 --- a/dce_plugin/docker_client.py +++ b/dce_plugin/docker_client.py @@ -1,9 +1,9 @@ import functools -import httplib +import http.client import json import os import socket -import urlparse +import urllib.parse from urllib import splitnport DEFAULT_TIMEOUT_SECONDS = 60 @@ -72,7 +72,7 @@ def parse_host(addr, is_win32=False, tls=False): host = address_parts[0] if len(address_parts) == 2: path = '/' + address_parts[1] - host, port = splitnport(host) + host, port = host.split(':') if port is None: raise DockerException( @@ -93,10 +93,10 @@ def parse_host(addr, is_win32=False, tls=False): return "{0}://{1}:{2}{3}".format(proto, host, port, path).rstrip('/') -class UnixHTTPConnection(httplib.HTTPConnection): - def __init__(self, socket_path, host=None, port=None, strict=None, timeout=None): +class UnixHTTPConnection(http.client.HTTPConnection): + def __init__(self, socket_path, host=None, port=None, timeout=None): host = host or 'localhost' - httplib.HTTPConnection.__init__(self, host, port=port, strict=strict, timeout=timeout) + http.client.HTTPConnection.__init__(self, host, port=port, timeout=timeout) self.socket_path = socket_path def connect(self): @@ -124,9 +124,9 @@ def __init__(self, base_url=None, timeout=DEFAULT_TIMEOUT_SECONDS): self.conn_fac = functools.partial(UnixHTTPConnection, socket_path, timeout=timeout) elif self.base_url.startswith('http://'): - _, netloc, _, _, _, _ = urlparse.urlparse(self.base_url) + _, netloc, _, _, _, _ = urllib.parse.urllib.parse(self.base_url) host, port = netloc.split(':') - self.conn_fac = functools.partial(httplib.HTTPConnection, host, port=port, timeout=timeout) + self.conn_fac = functools.partial(http.client.HTTPConnection, host, port=port, timeout=timeout) else: raise DockerException( "Invalid docker host: {}".format(self.base_url)