src/modules/pipeutils.py
changeset 3234 3a90dc0b66c9
parent 3203 31dde75a80aa
child 3274 e06a0700e218
equal deleted inserted replaced
3231:68bc503929c6 3234:3a90dc0b66c9
    67         print(client_rpc.add(1, 2))
    67         print(client_rpc.add(1, 2))
    68         del client_rpc
    68         del client_rpc
    69 """
    69 """
    70 
    70 
    71 from __future__ import print_function
    71 from __future__ import print_function
    72 import SocketServer
       
    73 import errno
    72 import errno
    74 import fcntl
    73 import fcntl
    75 import httplib
       
    76 import os
    74 import os
    77 import socket
    75 import socket
    78 import stat
    76 import stat
    79 import struct
    77 import struct
    80 import sys
    78 import sys
    94 # These includes make it easier for clients to catch the specific
    92 # These includes make it easier for clients to catch the specific
    95 # exceptions that can be raised by this module.
    93 # exceptions that can be raised by this module.
    96 #
    94 #
    97 # Unused import; pylint: disable=W0611
    95 # Unused import; pylint: disable=W0611
    98 from jsonrpclib import ProtocolError as ProtocolError1
    96 from jsonrpclib import ProtocolError as ProtocolError1
    99 from xmlrpclib import ProtocolError as ProtocolError2
    97 
       
    98 # import-error; pylint: disable=F0401
       
    99 # no-name-in-module; pylint: disable=E0611
       
   100 from six.moves import socketserver, http_client
       
   101 from six.moves.xmlrpc_client import ProtocolError as ProtocolError2
   100 # Unused import; pylint: enable=W0611
   102 # Unused import; pylint: enable=W0611
   101 
   103 
   102 # debugging
   104 # debugging
   103 pipeutils_debug = (os.environ.get("PKG_PIPEUTILS_DEBUG", None) is not None)
   105 pipeutils_debug = (os.environ.get("PKG_PIPEUTILS_DEBUG", None) is not None)
   104 
   106 
   330 
   332 
   331         def setsockopt(self, *args):
   333         def setsockopt(self, *args):
   332                 """set socket opt."""
   334                 """set socket opt."""
   333                 pass
   335                 pass
   334 
   336 
   335 
   337 # pylint seems to be panic about these.
   336 class PipedHTTPResponse(httplib.HTTPResponse):
   338 # PipedHTTP: Class has no __init__ method; pylint: disable=W0232
       
   339 # PipedHTTPResponse.begin: Attribute 'will_close' defined outside __init__;
       
   340 # pylint: disable=W0201
       
   341 class PipedHTTPResponse(http_client.HTTPResponse):
   337         """Create a httplib.HTTPResponse like object that can be used with
   342         """Create a httplib.HTTPResponse like object that can be used with
   338         a pipe as a transport.  We override the minimum number of parent
   343         a pipe as a transport.  We override the minimum number of parent
   339         routines necessary."""
   344         routines necessary."""
   340 
   345 
   341         def begin(self):
   346         def begin(self):
   342                 """Our connection will never be automatically closed, so set
   347                 """Our connection will never be automatically closed, so set
   343                 will_close to False."""
   348                 will_close to False."""
   344 
   349 
   345                 httplib.HTTPResponse.begin(self)
   350                 http_client.HTTPResponse.begin(self)
   346                 self.will_close = False
   351                 self.will_close = False
   347                 return
   352                 return
   348 
   353 
   349 
   354 
   350 class PipedHTTPConnection(httplib.HTTPConnection):
   355 class PipedHTTPConnection(http_client.HTTPConnection):
   351         """Create a httplib.HTTPConnection like object that can be used with
   356         """Create a httplib.HTTPConnection like object that can be used with
   352         a pipe as a transport.  We override the minimum number of parent
   357         a pipe as a transport.  We override the minimum number of parent
   353         routines necessary."""
   358         routines necessary."""
   354 
   359 
   355         # we use PipedHTTPResponse in place of httplib.HTTPResponse
   360         # we use PipedHTTPResponse in place of httplib.HTTPResponse
   357 
   362 
   358         def __init__(self, fd, port=None, strict=None):
   363         def __init__(self, fd, port=None, strict=None):
   359                 assert port is None
   364                 assert port is None
   360 
   365 
   361                 # invoke parent constructor
   366                 # invoke parent constructor
   362                 httplib.HTTPConnection.__init__(self, "localhost",
   367                 http_client.HTTPConnection.__init__(self, "localhost",
   363                     strict=strict)
   368                     strict=strict)
   364 
   369 
   365                 # self.sock was initialized by httplib.HTTPConnection
   370                 # self.sock was initialized by httplib.HTTPConnection
   366                 # to point to a socket, overwrite it with a pipe.
   371                 # to point to a socket, overwrite it with a pipe.
   367                 assert(type(fd) == int) and os.fstat(fd)
   372                 assert(type(fd) == int) and os.fstat(fd)
   438                 c = self.make_connection(host)
   443                 c = self.make_connection(host)
   439                 c.send(request_body)
   444                 c.send(request_body)
   440                 return self.parse_response(c.makefile())
   445                 return self.parse_response(c.makefile())
   441 
   446 
   442 
   447 
   443 class _PipedServer(SocketServer.BaseServer):
   448 class _PipedServer(socketserver.BaseServer):
   444         """Modeled after SocketServer.TCPServer."""
   449         """Modeled after SocketServer.TCPServer."""
   445 
   450 
   446         def __init__(self, fd, RequestHandlerClass):
   451         def __init__(self, fd, RequestHandlerClass):
   447                 self.__pipe_file = PipeFile(fd, "server-transport")
   452                 self.__pipe_file = PipeFile(fd, "server-transport")
   448                 self.__shutdown_initiated = False
   453                 self.__shutdown_initiated = False
   449 
   454 
   450                 SocketServer.BaseServer.__init__(self,
   455                 socketserver.BaseServer.__init__(self,
   451                     server_address="localhost",
   456                     server_address="localhost",
   452                     RequestHandlerClass=RequestHandlerClass)
   457                     RequestHandlerClass=RequestHandlerClass)
   453 
   458 
   454         def fileno(self):
   459         def fileno(self):
   455                 """Required to support select.select()."""
   460                 """Required to support select.select()."""