code: def get_esri_service_name(url):
      
      result = re.search('rest/services/(.*)/[MapServer|ImageServer]', url)
      if result is None:
          return url
      else:
          return result.group(1)
docs: A method to get a service name from an esri endpoint.
      For example: http://example.com/arcgis/rest/services/myservice/mylayer/MapServer/?f=json
      Will return: myservice/mylayer