@@ -243,11 +243,12 @@ def __init__(self, websocket, ports):
243243 # There is a thread run per PortForward instance which performs the translation between the
244244 # raw socket data sent by the python application and the websocket protocol. This thread
245245 # terminates after either side has closed all ports, and after flushing all pending data.
246- threading .Thread (
246+ proxy = threading .Thread (
247247 name = "Kubernetes port forward proxy: %s" % ', ' .join ([str (port ) for port in ports ]),
248- target = self ._proxy ,
249- daemon = True
250- ).start ()
248+ target = self ._proxy
249+ )
250+ proxy .daemon = True
251+ proxy .start ()
251252
252253 @property
253254 def connected (self ):
@@ -272,7 +273,7 @@ def __init__(self, ix, port_number):
272273 # The remote port number
273274 self .port_number = port_number
274275 # The websocket channel byte number for this port
275- self .channel = bytes ([ ix * 2 ] )
276+ self .channel = six . int2byte ( ix * 2 )
276277 # A socket pair is created to provide a means of translating the data flow
277278 # between the python application and the kubernetes websocket. The self.python
278279 # half of the socket pair is used by the _proxy method to receive and send data
@@ -350,9 +351,9 @@ def _proxy(self):
350351 if opcode == ABNF .OPCODE_BINARY :
351352 if not frame .data :
352353 raise RuntimeError ("Unexpected frame data size" )
353- channel = frame .data [ 0 ]
354+ channel = six . byte2int ( frame .data )
354355 if channel >= len (channel_ports ):
355- raise RuntimeError ("Unexpected channel number: " + str ( channel ) )
356+ raise RuntimeError ("Unexpected channel number: %s" % channel )
356357 port = channel_ports [channel ]
357358 if channel_initialized [channel ]:
358359 if channel % 2 :
@@ -366,14 +367,14 @@ def _proxy(self):
366367 raise RuntimeError (
367368 "Unexpected initial channel frame data size"
368369 )
369- port_number = frame .data [1 ] + (frame .data [2 ] * 256 )
370+ port_number = six . byte2int ( frame .data [1 : 2 ]) + (six . byte2int ( frame .data [2 : 3 ]) * 256 )
370371 if port_number != port .port_number :
371372 raise RuntimeError (
372- "Unexpected port number in initial channel frame: " + str ( port_number )
373+ "Unexpected port number in initial channel frame: %s" % port_number
373374 )
374375 channel_initialized [channel ] = True
375376 elif opcode not in (ABNF .OPCODE_PING , ABNF .OPCODE_PONG , ABNF .OPCODE_CLOSE ):
376- raise RuntimeError ("Unexpected websocket opcode: " + str ( opcode ) )
377+ raise RuntimeError ("Unexpected websocket opcode: %s" % opcode )
377378 else :
378379 port = local_ports [sock ]
379380 data = port .python .recv (1024 * 1024 )
@@ -383,8 +384,7 @@ def _proxy(self):
383384 ABNF .OPCODE_BINARY ,
384385 ).format ()
385386 else :
386- if not port .data :
387- port .python .close ()
387+ port .python .close ()
388388 for sock in w :
389389 if sock == self .websocket :
390390 sent = self .websocket .sock .send (kubernetes_data )
0 commit comments