def recvtill(s, target, count=1):
  """Receive data from the socket s until count occurence of target are in the buffer.
  Then it returns the buffer.

  Keyword arguments:
  s -- The socket to use
  target -- The target string
  count -- The number of occurrence of result needed (default 1)
  """
  done = False
  buf = ''
  while not done:
    r = s.recv(1000)
    if not r:
      return buf
    buf += r
    done = buf.count(target) == count
  return buf