docs: def get_port_from_port_server(portserver_address, pid=None):
      """Request a free a port from a system-wide portserver.
  
      This follows a very simple portserver protocol:
      The request consists of our pid (in ASCII) followed by a newline.
      The response is a port number and a newline, 0 on failure.
  
      This function is an implementation detail of pick_unused_port().
      It should not normally be called by code outside of this module.
  
      Args:
        portserver_address: The address (path) of a unix domain socket
          with which to connect to the portserver.  A leading '@'
          character indicates an address in the "abstract namespace."
          On systems without socket.AF_UNIX, this is an AF_INET address.
        pid: The PID to tell the portserver to associate the reservation with.
          If None, the current process's PID is used.
  
      Returns:
        The port number on success or None on failure.
      """
      if not portserver_address:
          return None
      # An AF_UNIX address may start with a zero byte, in which case it is in the
      # "abstract namespace", and doesn't have any filesystem representation.
      # See 'man 7 unix' for details.
      # The convention is to write '@' in the address to represent this zero byte.
      if portserver_address[0] == '@':
          portserver_address = '\0' + portserver_address[1:]
  
      if pid is None:
          pid = os.getpid()
  
      try:
          # Create socket.
          if hasattr(socket, 'AF_UNIX'):
              sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
          else:
              # fallback to AF_INET if this is not unix
              sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
          try:
              # Connect to portserver.
              sock.connect(portserver_address)
  
              # Write request.
              sock.sendall(('%d\n' % pid).encode('ascii'))
  
              # Read response.
              # 1K should be ample buffer space.
              buf = sock.recv(1024)
          finally:
              sock.close()
      except socket.error as e:
          print('Socket error when connecting to portserver:', e,
                file=sys.stderr)
          return None
  
      try:
          port = int(buf.split(b'\n')[0])
      except ValueError:
          print('Portserver failed to find a port.', file=sys.stderr)
          return None
      _owned_ports.add(port)
      return port