Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

修改urllib相关库,支持python3 #7

Open
wants to merge 1 commit into
base: sdk/3.0.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions dce_plugin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -111,15 +111,15 @@ 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()
)
return json.load(response)

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()
)
Expand Down
16 changes: 8 additions & 8 deletions dce_plugin/docker_client.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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(
Expand All @@ -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):
Expand Down Expand Up @@ -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)
Expand Down