import socket


def ParseProtocol(protocol_string):
  """Attempt to parse a protocol number from a string.

  Args:
    protocol_string: The string to parse.

  Returns:
    The corresponding protocol number.

  Raises:
    ValueError: If the protocol_string is not a valid protocol string.
  """
  try:
    protocol = socket.getprotobyname(protocol_string)
  except (socket.error, TypeError):
    try:
      protocol = int(protocol_string)
    except (ValueError, TypeError):
      raise ValueError('Invalid protocol: %s' % protocol_string)

  return protocol