Skip to content

Commit

Permalink
Support both python 2.7 and 3.x.
Browse files Browse the repository at this point in the history
  • Loading branch information
iciclespider committed Sep 8, 2020
1 parent d9ec734 commit c1249c9
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
7 changes: 4 additions & 3 deletions examples/pod_portforward.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
import select
import socket
import time
import urllib.request

import six.moves.urllib.request as urllib_request

from kubernetes import config
from kubernetes.client import Configuration
Expand Down Expand Up @@ -179,11 +180,11 @@ def kubernetes_create_connection(address, *args, **kwargs):

# Access the nginx http server using the
# "<pod-name>.pod.<namespace>.kubernetes" dns name.
response = urllib.request.urlopen(
response = urllib_request.urlopen(
'http://%s.pod.default.kubernetes' % name)
html = response.read().decode('utf-8')
response.close()
print('Status:', response.status)
print('Status Code: %s' % response.code)
print(html)


Expand Down
9 changes: 5 additions & 4 deletions kubernetes/e2e_test/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import socket
import time
import unittest
import urllib.request
import uuid

from kubernetes.client import api_client
Expand All @@ -26,6 +25,7 @@
from kubernetes.stream import stream, portforward
from kubernetes.stream.ws_client import ERROR_CHANNEL

import six.moves.urllib.request as urllib_request

def short_uuid():
id = str(uuid.uuid4())
Expand Down Expand Up @@ -228,7 +228,7 @@ def test_portforward_raw(self):
self.assertTrue(pf.connected)

sock = pf.socket(1236)
self.assertRaises(BrokenPipeError, sock.sendall, b'This should fail...')
self.assertRaises(socket.error, sock.sendall, b'This should fail...')
self.assertIsNotNone(pf.error(1236))
sock.close()

Expand All @@ -246,6 +246,7 @@ def test_portforward_raw(self):
reply += data
self.assertEqual(reply, sent)
sock.close()
time.sleep(1)
self.assertFalse(pf.connected)
self.assertIsNone(pf.error(1234))
self.assertIsNone(pf.error(1235))
Expand Down Expand Up @@ -300,12 +301,12 @@ def kubernetes_create_connection(address, *args, **kwargs):
socket_create_connection = socket.create_connection
try:
socket.create_connection = kubernetes_create_connection
response = urllib.request.urlopen('http://%s.default.kubernetes/' % name)
response = urllib_request.urlopen('http://%s.default.kubernetes/' % name)
html = response.read().decode('utf-8')
finally:
socket.create_connection = socket_create_connection

self.assertEqual(response.status, 200)
self.assertEqual(response.code, 200)
self.assertTrue('<h1>Welcome to nginx!</h1>' in html)

resp = api.delete_namespaced_pod(name=name, body={},
Expand Down

0 comments on commit c1249c9

Please sign in to comment.