code: def attach(self, host=None, source=None, sourcetype=None):
          
          args = { 'index': self.name }
          if host is not None: args['host'] = host
          if source is not None: args['source'] = source
          if sourcetype is not None: args['sourcetype'] = sourcetype
          path = UrlEncoded(PATH_RECEIVERS_STREAM + "?" + urllib.parse.urlencode(args), skip_encode=True)
  
          cookie_or_auth_header = "Authorization: Splunk %s\r\n" % \
                                  (self.service.token if self.service.token is _NoAuthenticationToken
                                   else self.service.token.replace("Splunk ", ""))
  
          # If we have cookie(s), use them instead of "Authorization: ..."
          if self.service.has_cookies():
              cookie_or_auth_header = "Cookie: %s\r\n" % _make_cookie_header(self.service.get_cookies().items())
  
          # Since we need to stream to the index connection, we have to keep
          # the connection open and use the Splunk extension headers to note
          # the input mode
          sock = self.service.connect()
          headers = [("POST %s HTTP/1.1\r\n" % str(self.service._abspath(path))).encode('utf-8'),
                     ("Host: %s:%s\r\n" % (self.service.host, int(self.service.port))).encode('utf-8'),
                     b"Accept-Encoding: identity\r\n",
                     cookie_or_auth_header.encode('utf-8'),
                     b"X-Splunk-Input-Mode: Streaming\r\n",
                     b"\r\n"]
  
          for h in headers:
              sock.write(h)
          return sock
docs: Opens a stream (a writable socket) for writing events to the index.
  
          :param host: The host value for events written to the stream.
          :type host: ``string``
          :param source: The source value for events written to the stream.
          :type source: ``string``
          :param sourcetype: The sourcetype value for events written to the
              stream.
          :type sourcetype: ``string``
  
          :return: A writable socket.